谁能帮小弟提供一个抓取网页数据的程序,最好是cSharp的!

gkmzk 2011-09-19 03:27:03
谁能帮小弟提供一个现在常用的抓取网页数据的程序,最好是cSharp的!
...全文
374 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
gkmzk 2011-09-27
  • 打赏
  • 举报
回复
那么如果说一个元素在页面上显示,而在其源文件找不出来,都有可能会是什么情况!
bill_poon 2011-09-26
  • 打赏
  • 举报
回复
{......
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(GetHtml(url));//获取网页源代码
string Url = string.Format("http://..../{0}", GetProperty(htmlNode, "//frame[@name='query']", "src"));//获取src中的"query.php"
........
}


private string GetProperty(HtmlNode htmlNode, string xpath, string property)
{
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlNode.OuterHtml);
HtmlNode target = htmlDoc.DocumentNode.SelectSingleNode(xpath);
if (target != null && target.Attributes[property] != null)
{
return target.Attributes[property].Value;
}
return null;
}
gkmzk 2011-09-26
  • 打赏
  • 举报
回复
我获到的html代码是这样的:
<html>
<head></head>
<body>
<frameset rows="300,*" frameboder="no" >
<frame name="query" src="query.php">
<frame name="result" >
<noframes>
<body>
</body>
</noframes>
</frame>
</frameset>
</body>
</html>
是个框架页,"query.php"是个查询页,查询结果应该就在以"result"为名的框架页中,但是我打不到它的"src"属性,问题就在这儿!
sxldfang 2011-09-25
  • 打赏
  • 举报
回复
18楼的代码你试了吗?不管是否catch到错误,都可继续利用response获取网页数据。

若不行,把你的网页地址放出来看看!
gkmzk 2011-09-24
  • 打赏
  • 举报
回复
其它的网站一定能行吗?,不过我想先获到到这个网站的数据!
萧炎 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 gkmzk 的回复:]
是可以获到到页面的html,只是说部分页面的数据在所获取到的html中找不到, 是这个意思!
[/Quote]
额-- 网站问题吧 你用其他网站试试
gkmzk 2011-09-24
  • 打赏
  • 举报
回复
是可以获到到页面的html,只是说部分页面的数据在所获取到的html中找不到, 是这个意思!
萧炎 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 gkmzk 的回复:]
引用 23 楼 lxxc11 的回复:
再给段我以前写的可以的获取的。你试试看可不可以。

C# code
static string GetHTML(string url)//获取网页源代码。
{
string Html = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.C……

这个代码我也用过了,不能获取……
[/Quote]
LZ我很纳闷 我那个代码 N个人用了都行 为什么你不行啊?

你确定你其他地方没搞错?
gkmzk 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 lxxc11 的回复:]
再给段我以前写的可以的获取的。你试试看可不可以。

C# code
static string GetHTML(string url)//获取网页源代码。
{
string Html = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.C……
[/Quote]
这个代码我也用过了,不能获取到页面显示的数据!
兰溪小城 2011-09-24
  • 打赏
  • 举报
回复
再给段我以前写的可以的获取的。你试试看可不可以。
         static string GetHTML(string url)//获取网页源代码。
{
string Html = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Get";
request.ContentType = "application/x-www-form-urlencoded ";
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, System.Text.Encoding.GetEncoding("gb2312"));
Html = sr.ReadToEnd();
return Html;
s.Close();
sr.Close();
}
萧炎 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 gkmzk 的回复:]
引用 19 楼 zyloveyrf 的回复:
C# code

//获取网站源码
private string GetWebContent(string sUrl)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest……

我用的就是这个代码,根本无法获到页面元素的html
[/Quote]
不是吧 我刚测试了 没问题啊 你的是窗体程序撒?
我在窗体程序中 完全没问题啊
兰溪小城 2011-09-24
  • 打赏
  • 举报
回复
因为有些网站有防止别人抓取信息的功能,所以有些就不可以了。比较普通的用上面那些代码就可以了。

你也可以看一下源文件,页面上显示的内容在源文件找不到的有些。那些HTTPREQUEST获取的只是源文件的内容
gkmzk 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 zyloveyrf 的回复:]
C# code

//获取网站源码
private string GetWebContent(string sUrl)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest……
[/Quote]
我用的就是这个代码,根本无法获到页面元素的html
萧炎 2011-09-24
  • 打赏
  • 举报
回复

//获取网站源码
private string GetWebContent(string sUrl)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
//声明一个HttpWebRequest请求
request.Timeout = 3000000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ToString() != "")
{
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("UTF-8");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
}
catch (Exception exp)
{
//MessageBox.Show("出错");
MessageBox.Show(exp.Message);
}
return strResult;
}
//测试:string str=GetWebContent("http://www.baidu.com");
sxldfang 2011-09-24
  • 打赏
  • 举报
回复
这是从我的程序中复制的部分内容,但很关键,希望能帮得到你!

HttpWebResponse response =null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch(WebException ex)
{
response = (HttpWebResponse)ex.Response;
}
gkmzk 2011-09-24
  • 打赏
  • 举报
回复
大哥们,我现在的问题是根本就获到不到html代码,所以根本不能通过html代码来获到其内部数据的,望大家谅解!
bill_poon 2011-09-22
  • 打赏
  • 举报
回复
public void main()
{
string url = "";
string main = ResponJson(url);
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(main);
string strTitle= GetText(htmlDoc.DocumentNode, "//div[@id='']");//用xpath提取题目
.......................

}

private string GetText(HtmlNode htmlNode, string xpath)
{
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlNode.OuterHtml);
HtmlNode target = htmlDoc.DocumentNode.SelectSingleNode(xpath);
if (target != null)
{
return target.InnerText.Trim();
}
return null;
}

public string ResponJson(string strUrl)
{
HttpWebRequest MyRequest = WebRequest.Create(strUrl) as HttpWebRequest;

MyRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

HttpWebResponse MyResponse = null;

MyResponse = MyRequest.GetResponse() as HttpWebResponse;


Stream myStream = MyResponse.GetResponseStream();

StreamReader myStreamReader = new StreamReader(myStream, System.Text.Encoding.GetEncoding("UTF-8"));

string readConnect = myStreamReader.ReadToEnd();

myStreamReader.Close();

MyResponse.Close();

return readConnect;
}




给你参考下
bill_poon 2011-09-22
  • 打赏
  • 举报
回复
自己写一个吧!利用正则或xpath把你想要的内容从网页上抓下来
yanran_hill 2011-09-22
  • 打赏
  • 举报
回复
string sServerUrl = @"...";
string ResponseStr;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sServerUrl);
try
{
using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
{
...
}
ResponseStr=...;
}

HTMLDocument document = new HTMLDocumentClass();
IHTMLDocument2 doc = (IHTMLDocument2)document;
try
{
string strResponse = ResponseStr;
doc.write(oPageText);
if (null != document)
{
}
}
catch
{
}

mshtml.HTMLDocumentClass logindocument = (mshtml.HTMLDocumentClass)document;
mshtml.HTMLFormElement loginform = (mshtml.HTMLFormElement)
logindocument.getElementById("loginform");
mshtml.HTMLInputElement username = (mshtml.HTMLInputElement) logindocument.getElementById("username");
string strusername = username.value;
gkmzk 2011-09-22
  • 打赏
  • 举报
回复
MSHTML主要是用来干什么的,怎么样获取不显示在 html中的数据!
加载更多回复(12)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧