Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从HTML字符串中提取HREF值_C#_Webclient Download - Fatal编程技术网

C# 从HTML字符串中提取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

我正在尝试创建一个只返回网站链接的爬虫程序,我知道它会返回HTML脚本。 我现在想使用if语句检查字符串是否返回,如果返回,它将搜索所有“”标记并显示href链接。 但我不知道要检查什么对象,或者应该检查什么值

以下是我到目前为止的情况:

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(页面内容,“,”)
这将为您提供一个列表,您可以在其中再次搜索每个
标记中的标记

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;
}
public List getbetweentag(字符串pageContents、字符串startTag、字符串endTag)
{
正则表达式rx=新正则表达式(startTag+“(.*?”+endTag);
MatchCollection col=rx.Matches(值);
列表标签=新列表();
foreach(在col中匹配s)
tags.Add(s.ToString());
返回标签;
}
编辑:哇哦,我不知道HTML Agility Pack,谢谢@Gauravsa,我会更新我的项目来使用它

您可以看一下包装:

然后,您可以从网页中找到所有链接,如:

 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);
 }
var hrefs=newlist();
var hw=新的HtmlWeb();
HtmlDocument document=hw.Load(/*您在此处的url*/);
foreach(document.DocumentNode.SelectNodes(“//a[@href]”)中的HtmlNode链接)
{
HtmlAttribute属性=link.Attributes[“href”];
如果(!string.IsNullOrWhiteSpace(attribute.Value))
添加(attribute.Value);
}

您是否尝试了
if(string.IsNullOrEmpty(WebData))
?我使用了null或空检查,它似乎工作正常,谢谢!是的,如果有人打算在非常基本的东西之外进行任何网站操作/抓取,我强烈建议使用Agility Pack。Alex,这段代码非常出色,因为我正在寻找一种在SSIS包的脚本任务中实现这一点的方法,而不需要任何外部依赖!谢谢
 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);
 }