按文件扩展名过滤网页上的链接
filter links on page web by files extensions
我正在研究从网页中提取链接并使用 HtmlAgilityPack 过滤它们的程序
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(txt_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("a//[@href]"))
{
// Get the value of the HREF attribute
string hrefValue = link.GetAttributeValue("href", string.Empty);
listbox1.Items.Add(hrefValue);
}
此代码从网页中提取所有链接,所以我的问题是如何通过扩展名过滤这些 Urls,例如“.html”
使用WebClient.DownloadString方法得到html.
然后在字符串上使用正则表达式模式来捕获所有 URL。
首先,您必须执行 HTTP GET 请求并使用 HTML 代码获取响应正文。
//Request HTTP GET
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = "GET";
WebResponse response;
string html = "";
response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
html = sr.ReadToEnd();
sr.Close();
response.Close();
然后您可以使用正则表达式解析 HTML 代码以提取所需的文件。
我正在研究从网页中提取链接并使用 HtmlAgilityPack 过滤它们的程序
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(txt_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("a//[@href]"))
{
// Get the value of the HREF attribute
string hrefValue = link.GetAttributeValue("href", string.Empty);
listbox1.Items.Add(hrefValue);
}
此代码从网页中提取所有链接,所以我的问题是如何通过扩展名过滤这些 Urls,例如“.html”
使用WebClient.DownloadString方法得到html.
然后在字符串上使用正则表达式模式来捕获所有 URL。
首先,您必须执行 HTTP GET 请求并使用 HTML 代码获取响应正文。
//Request HTTP GET
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = "GET";
WebResponse response;
string html = "";
response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
html = sr.ReadToEnd();
sr.Close();
response.Close();
然后您可以使用正则表达式解析 HTML 代码以提取所需的文件。