开发者

Htmlnode collection and parsing

开发者 https://www.devze.com 2022-12-08 04:02 出处:网络
I\'m trying to extract the text contained in a webpage. So that I\'m using a third pary tool Html Agility Pack. In that they mentioned:

I'm trying to extract the text contained in a webpage. So that I'm using a third pary tool Html Agility Pack. In that they mentioned:

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDo开发者_运维百科cument doc = htmlWeb.Load("http://www.msn.com/");

HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a[@href]");
foreach (HtmlNode link in links)
{
Response.Write(link.Attributes["href"].Value + "<br>");
}

It is working for me to grab all other links contained in a page. But I want to get all the text data contained in that page. Is it possible?


Yep, it's possible. Download the source code for the HtmlAgilityPack and take a look at the Html2Txt sample project, particularly HtmlConvert.cs. You can pretty much copy/paste their method into whatever it is you're doing.

Or, for that matter, compile the sample project as-is and set a reference to the binaries. HtmlAgilityPack.Samples.HtmlToText.Convert() will do exactly what you need.


you are using an xpath selector there. If you select all nodes ("*") and then perform the foreach would it work?

PS: what programming language is this?

0

精彩评论

暂无评论...
验证码 换一张
取 消