Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 用HAP搜索google_C#_Html_Html Agility Pack - Fatal编程技术网

C# 用HAP搜索google

C# 用HAP搜索google,c#,html,html-agility-pack,C#,Html,Html Agility Pack,如何从获取href属性 这是我的代码-它使用HtmlAgilityPack加载HTML源代码,但是当在foreach循环中迭代SelectNodes(“//a[@class='l']”)时,不起作用 有什么想法吗 HtmlWeb siec = new HtmlWeb(); HtmlAgilityPack.HtmlDocument htmldokument = siec.Load(@"https://www.google.pl/search?q=beer"); List<string>

如何从
获取href属性

这是我的代码-它使用HtmlAgilityPack加载HTML源代码,但是当在foreach循环中迭代
SelectNodes(“//a[@class='l']”)时,
不起作用

有什么想法吗

HtmlWeb siec = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument = siec.Load(@"https://www.google.pl/search?q=beer");
List<string> list = new List<string>();

if (htmldokument != null)
{
    foreach (HtmlNode text in htmldokument.DocumentNode.SelectNodes("//a[@class='l']"))
    {

        list.Add(text.InnerHtml);
        Console.WriteLine(text.InnerHtml);
    }
}
Console.ReadKey();
HtmlWeb-siec=新的HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument=siec.Load(@)https://www.google.pl/search?q=beer");
列表=新列表();
如果(htmldokument!=null)
{
foreach(htmldokument.DocumentNode.SelectNodes(“//a[@class='l']”)中的HtmlNode文本)
{
添加(text.InnerHtml);
Console.WriteLine(text.InnerHtml);
}
}
Console.ReadKey();

您可以访问如下HtmlNode属性:

node.Attributes[“name”].Value

或者更好(以防属性不存在):

如果未找到属性
name
,将返回一个空字符串


HtmlWeb-siec=新的HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument=siec.Load(@)https://www.google.pl/search?q=beer");
列表=新列表();
如果(htmldokument!=null)
{
foreach(htmldokument.DocumentNode.SelectNodes(“//a[@class='l']”)中的HtmlNode文本)
{
var href=text.GetAttributeValue(“href”,”);
list.Add(href);
Console.WriteLine(href);
}
}
Console.ReadKey();

您可以访问如下HtmlNode属性:

node.Attributes[“name”].Value

或者更好(以防属性不存在):

如果未找到属性
name
,将返回一个空字符串


HtmlWeb-siec=新的HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument=siec.Load(@)https://www.google.pl/search?q=beer");
列表=新列表();
如果(htmldokument!=null)
{
foreach(htmldokument.DocumentNode.SelectNodes(“//a[@class='l']”)中的HtmlNode文本)
{
var href=text.GetAttributeValue(“href”,”);
list.Add(href);
Console.WriteLine(href);
}
}
Console.ReadKey();
node.GetAttributeValue("name", "")
HtmlWeb siec = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument = siec.Load(@"https://www.google.pl/search?q=beer");
List<string> list = new List<string>();

if (htmldokument != null)
{
    foreach (HtmlNode text in htmldokument.DocumentNode.SelectNodes("//a[@class='l']"))
    {
        var href = text.GetAttributeValue("href", "");
        list.Add(href);
        Console.WriteLine(href);
    }
}
Console.ReadKey();