最近要实现一个动态抓取其他网站的内容,然后将该内容放在自己的网站上,供浏览者浏览.
比如我要抓取http://www.china-cet.com/网站上的--考试策略栏目.
不但要抓取信息标题,而且要抓取该信息的内容.我觉的可以用JAVASCRIPT的方式来实现.不知道大家觉得可行么..可以的话应该用什么思路呢.
如果有源码供我参考的话,就更好了..不知道大家原来有没做过类似的东西,或者有更好的实现思路..还望指点下..
比如我要抓取http://www.china-cet.com/网站上的--考试策略栏目.
不但要抓取信息标题,而且要抓取该信息的内容.我觉的可以用JAVASCRIPT的方式来实现.不知道大家觉得可行么..可以的话应该用什么思路呢.
如果有源码供我参考的话,就更好了..不知道大家原来有没做过类似的东西,或者有更好的实现思路..还望指点下..
这个 我研究过 可以
WebResponse wrs = wrq.GetResponse();
Stream strm = wrs.GetResponseStream();
StreamReader sr = new StreamReader(strm, Encoding.GetEncoding("gb2312"));//中文网站大多是这个格式
string line;
while ((line = sr.ReadLine()) != null)
{
line += line;
}
//以上就是获取源文件啦。接下去就是substring,Split,Replace这些方法啦。
try{}catch(){}哦。
//转换输入参数的编码类型
string mycity = System.Web.HttpUtility.UrlEncode(city, System.Text.UnicodeEncoding.GetEncoding("GB2312"));
//初始化新的WebRequest
HttpWebRequest webrt = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city="+mycity);
//返回对Internet请求响应
HttpWebResponse webrs = (HttpWebResponse)webrt.GetResponse();
//返回Internet资源返回数据流
Stream stream = webrs.GetResponseStream(); //读取数据流
StreamReader srm = new StreamReader(stream, System.Text.Encoding.Default);
//从头到尾,把数据读到weatherhtml中
weatherhtml = srm.ReadToEnd();
//关闭打开的资源
srm.Close();
stream.Close();
webrs.Close();
//针对不同的网站,以下开始部分和结束部分不同
//可以通过查看网站的源文件解决
int start = weatherhtml.IndexOf("城市天气 begin");
int end = weatherhtml.IndexOf("城市天气 end");
//返回一个html的table,预报城市天气
return weatherhtml.Substring(start + 14,end-start);
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Net;namespace mwcj
{
class ClsThief
{
/// <summary>
/// 获取特定网页源代码
/// </summary>
/// <param name="url">要获取的网页地址</param>
/// <returns></returns>
public static string tou(string url)
{
string temp = "";
try
{
//根据指定的UR建立WEB请求
WebRequest request = WebRequest.Create(url);
//定义对上面WEB请求的反应
WebResponse response = request.GetResponse();
//取得WEB响应的数据流
Stream resStream = response.GetResponseStream();
//根据上面定义的数据流,以默认编码的方式定义一个读数据流
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);//.Default);
// 读取数据流中的内容
temp = sr.ReadToEnd();
//关闭数据流
resStream.Close();
//关闭读数据流并释放资源
sr.Close();
}
catch { }
return temp;
}
/// <summary>
/// 获取特定网页源代码
/// </summary>
/// <param name="httpUrl">要获取的网页地址</param>
/// <returns></returns>
public static string GetUrlCode(string httpUrl)
{
string T_str = "";
try
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] PageData = wc.DownloadData(httpUrl);
T_str = Encoding.Default.GetString(PageData);
}
catch
{ }
return T_str;
}
/// <summary>
/// 去除字符串中所有的特定标记之间的内容
/// </summary>
/// <param name="str">要去除的字符串 如a11b34a34b</param>
/// <param name="str1">开始标记如a</param>
/// <param name="str2">结束标记如b</param>
/// <returns>去除后的字符串 如1134</returns>
public static string CutSpan(string str, string str1, string str2)
{
string temp = str;
string a = StrCut(str, str1, str2, 0);
if (a != "")
{
temp = temp.Replace(str1 + a + str2, "");
temp = CutSpan(temp, str1, str2);
}
return temp;
}
/// <summary>
/// 截取字符串特定字符之间的字符串
/// </summary>
/// <param name="str">要截取的字符串 如abcdefg</param>
/// <param name="str1">开始截取的字符串,如b</param>
/// <param name="str2">结束截取的字符串 如f</param>
/// <param name="type">去除类型 0只取中间 1 包含开始的 2 包含结束的 else 包含开始和结束的</param>
/// <returns>得到截取后的字符串,如cde</returns>
public static string StrCut(string str, string str1, string str2, int type)
{
string temp = "";
int ad1 = str.IndexOf(str1, 0);
if (ad1 == -1)
{
temp = "";
}
else
{
int ad2 = str.IndexOf(str2, ad1 + str1.Length);
int ad3 = ad2 - ad1;
string t = str.Substring(ad1, ad3);
t = t.Replace(str1, "");
temp = t.ToString(); switch (type)
{
case 0://只取中间的 break;
case 1: //包含开始的
temp = str1 + temp;
break;
case 2://包含结束的
temp = temp + str2;
break;
case 3://包含开始和结束的
temp = str1 + temp + str2;
break;
}
}
return temp;
}
}}
WebResponse wrs = wrq.GetResponse();
Stream strm = wrs.GetResponseStream();
StreamReader sr = new StreamReader(strm, Encoding.GetEncoding("gb2312"));//中文网站大多是这个格式
string line;
while ((line = sr.ReadLine()) != null)
{
line += line;
}
获取以后 用正则匹配链接
然后采集
里面就有这功能下载地址:
http://blog.csdn.net/Feiin/archive/2008/08/24/2823934.aspx
不过好像都是用java的,我以前用c++写过一个模拟浏览器下载http文件的程序,
将文件下载后,在解析一下字符串即可~
如果直接用js去分析网页,我觉得太麻烦。
这个基本上可以,个别的地方可能还需要自己处理一下。