1

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

我只想获取包含的值docid=

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64556e8988583f

#

summary_of_documents.php

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64579b861c1d7b

#

index.php?pageid=a45475a11ec72b843d74959b60fd7bd64579e0509c7f0&apform=司法

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");
        }

我怎样才能做到这一点?

4

1 回答 1

2

为什么不直接替换这个:

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

有了这个:

if (nd.Attributes["href"].Value.Contains("docid="))
    links.Add(nd.Attributes["href"].Value);
于 2012-04-11T08:38:04.440 回答