C# 从HTML字符串中提取HREF值
我正在尝试创建一个只返回网站链接的爬虫程序,我知道它会返回HTML脚本。 我现在想使用if语句检查字符串是否返回,如果返回,它将搜索所有“”标记并显示href链接。 但我不知道要检查什么对象,或者应该检查什么值 以下是我到目前为止的情况:C# 从HTML字符串中提取HREF值,c#,webclient-download,C#,Webclient Download,我正在尝试创建一个只返回网站链接的爬虫程序,我知道它会返回HTML脚本。 我现在想使用if语句检查字符串是否返回,如果返回,它将搜索所有“”标记并显示href链接。 但我不知道要检查什么对象,或者应该检查什么值 以下是我到目前为止的情况: namespace crawler { class Program { static void Main(string[] args) { System.Net.WebClient wc
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
}
}
}
首先,您可以创建一个函数来返回整个网站的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;
}
然后,您可以创建一个返回子字符串或子字符串列表的函数(这意味着如果您想要所有标记,您可能会得到多个)
List divTags=getbetweenttags(页面内容,“,”)
这将为您提供一个列表,您可以在其中再次搜索每个