【问题标题】:HTML Agility Pack - Filter Href Value ResultsHTML 敏捷包 - 过滤 Href 值结果
【发布时间】:2012-04-11 08:28:00
【问题描述】:

我正在开发一个网络爬虫。以下文本显示了本题末尾给出的代码的结果,该代码从页面中获取所有href的值。

我只想获取包含docid=的值

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64556e8988583f

#

summary_of_documents.php

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64579b861c1d7b

#

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64579e0509c7f0&apform=judiciary

decisions.php?doctype=Decisions / 签名 分辨率&docid=1263778435388003271#sam

decisions.php?doctype=Decisions / 签名 分辨率&docid=12637789021669321156#sam

?doctype=Decisions / Signed Resolutions&year=1986&month=January#head

?doctype=Decisions / Signed Resolutions&year=1986&month=February#head

代码如下:

        string url = urlTextBox.Text;
        string sourceCode = Extractor.getSourceCode(url);

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(sourceCode);
        List<string> links = new List<string>();

        if (links != null)
        {
            foreach (HtmlAgilityPack.HtmlNode nd in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                links.Add(nd.Attributes["href"].Value);
            }
        }
        else
        {
            MessageBox.Show("No Links Found");
        }

        if (links != null)
        {
            foreach (string str in links)
            {
                richTextBox9.Text += str + "\n";
            }
        }
        else
        {
            MessageBox.Show("No Link Values Found");
        }

我该怎么做?

【问题讨论】:

  • 我在这里做了一些修改。请仔细检查:)

标签: c# web-scraping html-agility-pack


【解决方案1】:

为什么不直接替换这个:

links.Add(nd.Attributes["href"].Value);

用这个:

if (nd.Attributes["href"].Value.Contains("docid="))
    links.Add(nd.Attributes["href"].Value);

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2017-06-21
    相关资源
    最近更新 更多