OK Ive得到这个代码:

public static string ScreenScrape(string url)
    {
        System.Net.WebRequest request = System.Net.WebRequest.Create(url);
        // set properties of the request
        using (System.Net.WebResponse response = request.GetResponse())
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }
    }

现在我要过滤的文本,以获得DIV CLASS =“评论”的人 有另一种选项比使用正则表达式等?或者是,唯一的方法是什么?

感谢

有帮助吗?

解决方案

您需要使用 HTML敏捷性包

例如:

var doc = new HtmlWeb().Load(url);
var comments = doc.Descendants("div")
                  .Where(div => div.GetAttributeValue("class", "") == "comment");

请注意,这将无法找到<div class="OtherClass comment">;如果您正在寻找的是,你可以调用IndexOf

其他提示

HtmlAgilityPack仅仅是一个包,让您操作HTML文件,但是如果你想要做的屏幕抓取硒的webdriver与PhantomJS是更好的解决方案。 PhantomJS是无头的Web浏览器,因此是非常快。此外,相较于HTML敏捷包具有更好的功能。有一个短当然关于此主题。

您呼叫的第一端口应 HTML敏捷性包

正则表达式是解析这种输入的用于非.NET语言的传统方式。

Additionaly,如果可以这正常化到XML的变体(即XHTML),则可以使用XPATH查询和检索所需的节点。

你不想做的就是实现自己的解析器什么。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top