HTMLAgilityPack的SelectNodes选择所有< IMG>分子 [英] HTMLAgilityPack SelectNodes to select all <img> elements
问题描述
我正在在C#中的一个项目,基本上是一个图像搜索相关游戏的图像画面刮刀。我试图用HTMLAgilityPack选择所有图像元素,并把它们在HTMLNodeCollection,像这样的:
I am making a project in C# that's basically an image screen scraper for an image-search related game. I'm trying to use HTMLAgilityPack to select all the image elements and put them in an HTMLNodeCollection, like this:
//set up for checking autos
HtmlNodeCollection imgs = new HtmlNodeCollection(doc.DocumentNode.ParentNode);
imgs = doc.DocumentNode.SelectNodes("//img");
foreach (HtmlNode img in imgs)
{
HtmlAttribute src = img.Attributes["@src"];
urls.Add(src.Value);
}
注意网址的是一个公开名单集合:
Note that urls is a public List collection:
public List<string> urls = new List<string>();
我的foreach循环抛出一个异常:
My foreach loop is throwing an exception:
对象引用未设置到对象的实例。
Object reference not set to an instance of an object.
检查汽车,果然,的 IMGS 的为空。有没有更好的办法可以追查这个问题的根源?我不知道,如果是我的XPath或内容。
Checking the autos, sure enough, imgs is null. Is there any better way I can track down the source of this problem? I have no idea if it's my Xpath or what.
最令人沮丧的是,我已经得到了它的工作,但搞砸了我的文件版本,失去了我的工作。 。DERP
The most frustrating part is that I had already gotten it to work, but messed up my file versions and lost my work. Derp.
推荐答案
您可能有以下行一个错字:
You might have a typo in the following line:
HtmlAttribute src = img.Attributes["@src"];
我得到这个工作对我来说(注意@位置):
I got this to work for me (notice the @ position):
HtmlAttribute src = img.Attributes[@"src"];
这篇关于HTMLAgilityPack的SelectNodes选择所有< IMG>分子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!