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

gkmzk 2011-09-19 03:27:03
谁能帮小弟提供一个现在常用的抓取网页数据的程序,最好是cSharp的!
...全文
395 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)
C#串口介绍以及简单串口通信程序设计实现 源代码和串口程序介绍连接:https://www.cnblogs.com/JiYF/p/6618696.html 本站积分太贵,自己变得。。直接到连接地址下载代码 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍   串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度) 串口应用:   工业领域使用较多,比如:数据采集,设备控制等等,好多都是用串口通信来实现!你要是细心的话,你会发现,目前家用国网智能电能表就具备RS485通信总线(串行总线的一种)与RS232可以相互转化(当然一般,非专业的谁也不会闲的蛋疼,趴电表上瞎看,最多也就看看走了多少度电) RS232 DB9介绍: 1.示意图 2.针脚介绍: 载波检测(DCD) 接受数据(RXD) 发出数据(TXD) 数据终端准备好(DTR) 信号地线(SG) 数据准备好(DSR) 请求发送(RTS) 清除发送(CTS) 振铃指示(RI) 3.实物图: 以下是我购买XX公司的一个usb转串口线:这个头就是一个公头,另一端是一个usb口 笨小孩串口工具运行图: 1.开启程序 2.发送一行字符串HelloBenXH,直接将针脚的发送和接收链接起来就可以测试了(针脚2 接受数据(RXD) 和3 发出数据(TXD))直接链接, C#代码实现:采用SerialPort 1.实例化一个SerialPort [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 private SerialPort ComDevice = new SerialPort(); 2.初始化参数绑定接收数据事件 [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 public void init() { btnSend.Enabled = false; cbbComList.Items.AddRange(SerialPort.GetPortNames()); if (cbbComList.Items.Count > 0) { cbbComList.SelectedIndex = 0; } cbbBaudRate.SelectedIndex = 5; cbbDataBits.SelectedIndex = 0; cbbParity.SelectedIndex = 0; cbbStopBits.SelectedIndex = 0; pictureBox1.BackgroundImage = Properties.Resources.red; ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件 }

111,120

社区成员

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

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

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