如何将string转成HtmlDocument?

jacksoncan 2012-08-26 12:33:54
用WebRequest获得了一个网页html的string,(代码如下:)现在想将它转成HtmlDocument,不想用到webBrowser,因为webBrowser实在太慢,太多问题要处理; 然而HtmlDocument却无法通过简单的new关键字来创建,该怎么办?

或者,有什么类似HtmlDocument的东东可以方便地处理用WebRequest获得的html的string?也不想用正则表达式来处理,因为感觉太难写了,没有HtmlDocument里面的GetElementById之类的方法那么简单好用啊。



string url = "http://stock.finance.sina.com.cn/hkstock/finance/01398.html";

WebRequest request = WebRequest.Create(url); //请求url
WebResponse response = request.GetResponse(); //获取url数据

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string tempStr = reader.ReadToEnd();

...全文
2719 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
葡萄塘 2014-01-07
  • 打赏
  • 举报
回复
怎么用的楼主贴出来,求助!谢谢
jacksoncan 2012-08-26
  • 打赏
  • 举报
回复
大神,我知道该怎么用了,非常感谢你!!

[Quote=引用 1 楼 的回复:]

你用HtmlAgilityPack试试,下载地址http://htmlagilitypack.codeplex.com/

使用如下C# code
HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.Load(Server.MapPath("~/test.txt")); HtmlNodeCollection nodes = htmlDoc……
[/Quote]
jacksoncan 2012-08-26
  • 打赏
  • 举报
回复
非常感谢,但是又出现了一个问题啊,就是这一句之后,

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(@"//table//tr");
系统老是提示要用new关键字,但是用了new关键字之后,编译又过去不,这是怎么一回事呢?



string url = "http://www.aastocks.com/SC/Stock/CompanyFundamental.aspx?CFType=8&symbol=00001";

WebRequest request = WebRequest.Create(url); //请求url
WebResponse response = request.GetResponse(); //获取url数据

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string tempStr = reader.ReadToEnd();

this.richTextBox1.Text = tempStr; //将数据写入到textbox中

StringBuilder str = new StringBuilder();

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(@"//table//tr");


str.Append(nodes.Count.ToString());




[Quote=引用 1 楼 的回复:]

你用HtmlAgilityPack试试,下载地址http://htmlagilitypack.codeplex.com/

使用如下C# code
HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.Load(Server.MapPath("~/test.txt")); HtmlNodeCollection nodes = htmlDoc……
[/Quote]
  • 打赏
  • 举报
回复
你用HtmlAgilityPack试试,下载地址http://htmlagilitypack.codeplex.com/

使用如下
 HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.Load(Server.MapPath("~/test.txt")); HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes(@"//tbody//tr"); foreach (HtmlNode node in nodes) { Response.Write(node.SelectSingleNode(@"td[1]/a").Attributes["href"].Value + "<br/>"); Response.Write(node.SelectSingleNode(@"td[1]/a").InnerText + "<br/>"); Response.Write(node.SelectSingleNode(@"td[2]/span").InnerText + "<br/>"); Response.Write(node.SelectSingleNode(@"td[3]").InnerText + "<br/>"); Response.Write(node.SelectSingleNode(@"td[4]/span").InnerText + "<br/>"); }

110,566

社区成员

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

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

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