从 HTML 字符串中提取 HREF 值
Extract HREF values from HTML string
我正在尝试创建一个 returns 仅来自网站的 link 的抓取工具,我已经达到 returns HTML 脚本的程度。
我现在想使用 if 语句来检查是否返回了字符串,如果返回了,它会搜索所有“”标签并显示 href link。
但我不知道要检查什么对象或我应该检查什么值。
这是我目前的情况:
namespace crawler
{
class Program
{
static void Main(string[] args)
{
System.Net.WebClient wc = new System.Net.WebClient();
string WebData wc.DownloadString("https://www.abc.net.au/news/science/");
Console.WriteLine(WebData);
// if
}
}
}
首先,您可以像您所做的那样为 return 整个网站 HTML 代码创建一个函数。这是我的!
public string GetPageContents()
{
string link = "https://www.abc.net.au/news/science/"
string pageContent = "";
WebClient web = new WebClient();
Stream stream;
stream = web.OpenRead(link);
using (StreamReader reader = new StreamReader(stream))
{
pageContent = reader.ReadToEnd();
}
stream.Close();
return pageContents;
}
然后你可以创建一个函数,该函数将 return 一个子字符串或一个子字符串列表(这意味着如果你想要所有 标签,你可能会得到不止一个)。
List<string> divTags = GetBetweenTags(pageContents, "<div>", "</div>")
这将为您提供一个列表,例如,您可以在其中再次搜索每个 < div > 标签中的 < a > 标签。
public List<string> GetBetweenTags(string pageContents, string startTag, string endTag)
{
Regex rx = new Regex(startTag + "(.*?)" + endTag);
MatchCollection col = rx.Matches(value);
List<string> tags = new List<string>();
foreach(Match s in col)
tags.Add(s.ToString());
return tags;
}
编辑:哇不知道HTML Agility Pack,谢谢@Gauravsa 我会更新我的项目以使用它!
你可以看看HTML Agility包:
然后您可以找到网页中的所有链接,例如:
var hrefs = new List<string>();
var hw = new HtmlWeb();
HtmlDocument document = hw.Load(/* your url here */);
foreach(HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute attribute = link.Attributes["href"];
if (!string.IsNullOrWhiteSpace(attribute.Value))
hrefs.Add(attribute.Value);
}
我正在尝试创建一个 returns 仅来自网站的 link 的抓取工具,我已经达到 returns HTML 脚本的程度。 我现在想使用 if 语句来检查是否返回了字符串,如果返回了,它会搜索所有“”标签并显示 href link。 但我不知道要检查什么对象或我应该检查什么值。
这是我目前的情况:
namespace crawler
{
class Program
{
static void Main(string[] args)
{
System.Net.WebClient wc = new System.Net.WebClient();
string WebData wc.DownloadString("https://www.abc.net.au/news/science/");
Console.WriteLine(WebData);
// if
}
}
}
首先,您可以像您所做的那样为 return 整个网站 HTML 代码创建一个函数。这是我的!
public string GetPageContents()
{
string link = "https://www.abc.net.au/news/science/"
string pageContent = "";
WebClient web = new WebClient();
Stream stream;
stream = web.OpenRead(link);
using (StreamReader reader = new StreamReader(stream))
{
pageContent = reader.ReadToEnd();
}
stream.Close();
return pageContents;
}
然后你可以创建一个函数,该函数将 return 一个子字符串或一个子字符串列表(这意味着如果你想要所有 标签,你可能会得到不止一个)。
List<string> divTags = GetBetweenTags(pageContents, "<div>", "</div>")
这将为您提供一个列表,例如,您可以在其中再次搜索每个 < div > 标签中的 < a > 标签。
public List<string> GetBetweenTags(string pageContents, string startTag, string endTag)
{
Regex rx = new Regex(startTag + "(.*?)" + endTag);
MatchCollection col = rx.Matches(value);
List<string> tags = new List<string>();
foreach(Match s in col)
tags.Add(s.ToString());
return tags;
}
编辑:哇不知道HTML Agility Pack,谢谢@Gauravsa 我会更新我的项目以使用它!
你可以看看HTML Agility包:
然后您可以找到网页中的所有链接,例如:
var hrefs = new List<string>();
var hw = new HtmlWeb();
HtmlDocument document = hw.Load(/* your url here */);
foreach(HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute attribute = link.Attributes["href"];
if (!string.IsNullOrWhiteSpace(attribute.Value))
hrefs.Add(attribute.Value);
}