これは私が持っているコードです。私はURLからすべてのimg srcを取得する方法を学ぼうとしています。しかし、imgs私の下のコードの変数は常にnullです。私は間違って何をしていますか?c#HtmlAgility Pack - 画像を取得できません。src
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("http://archive.ncsa.illinois.edu/primer.html");
HtmlAgilityPack.HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img");
if (imgs != null)
{
foreach (HtmlAgilityPack.HtmlNode img in imgs)
{
string imgSrc = img.Attributes["src"].Value;
}
}
Console.ReadKey();
}
}
}
本当にnullですか?あなたはsrc属性で何もしません。それを印刷してみてください: 'Console.WriteLine(imgSrc);' – alexn
はい、私はなぜそれがnullであると確信しているのですか?私がデバッグすると、 "if(imgs!= null)"の内部にさえ入っていないからです。 「if(imgs!= null)」という行をスキップしてConsole.ReadKey()に移動します。すぐに! –