我正在研究一个网络刮刀。以下文本显示了此问题末尾给出的代码的结果,该代码获取了页面中所有href的值。
我只想获取包含docid=
的index.php?的pageid = a45475a11ec72b843d74959b60fd7bd64556e8988583f
#
summary_of_documents.php
的index.php?的pageid = a45475a11ec72b843d74959b60fd7bd64579b861c1d7b
#
的index.php的pageid = a45475a11ec72b843d74959b60fd7bd64579e0509c7f0&安培; apform =司法
decisions.php?doctype =决定/签名 决议&安培;文档ID = 1263778435388003271#SAM
decisions.php?doctype =决定/签名 决议&安培;文档ID =#12637789021669321156 SAM
?doctype =决定/签署的决议和年份= 1986年和月份= 1月#head
?doctype =决定/签署的决议和年份= 1986年和月份= 2月#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");
}
我该怎么做?
答案 0 :(得分:2)
为什么不直接替换它:
links.Add(nd.Attributes["href"].Value);
用这个:
if (nd.Attributes["href"].Value.Contains("docid="))
links.Add(nd.Attributes["href"].Value);