C# HTMLAgilityPack 获取 src。 xpath 无效
C# HTMLAgilityPack getting src. xpath is not valid
我无法获得正确的 xpath。我正在尝试获取任何 IMDB 电影的图像,但它似乎不起作用。这是我的代码。
// Getting the node
HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[@id=\"title - overview - widget\"]/div[2]/div[3]/div[1]/a/img");
// Getting the attribute data
HtmlAttributeCollection attr = node.Attributes;
该属性为空。每次但是。 xpath 不起作用,我不知道为什么。我觉得不错。
您可以使用更简单的 xpath
var url = "http://www.imdb.com/title/tt0816692/";
using (var client = new HttpClient())
{
var html = await client.GetStringAsync(url);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var img = doc.DocumentNode.SelectSingleNode("//img[@title='Trailer']")
?.Attributes["src"]?.Value;
//or
var poster = doc.DocumentNode.SelectSingleNode("//div[@class='poster']//img")
?.Attributes["src"]?.Value;
}
我无法获得正确的 xpath。我正在尝试获取任何 IMDB 电影的图像,但它似乎不起作用。这是我的代码。
// Getting the node
HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[@id=\"title - overview - widget\"]/div[2]/div[3]/div[1]/a/img");
// Getting the attribute data
HtmlAttributeCollection attr = node.Attributes;
该属性为空。每次但是。 xpath 不起作用,我不知道为什么。我觉得不错。
您可以使用更简单的 xpath
var url = "http://www.imdb.com/title/tt0816692/";
using (var client = new HttpClient())
{
var html = await client.GetStringAsync(url);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var img = doc.DocumentNode.SelectSingleNode("//img[@title='Trailer']")
?.Attributes["src"]?.Value;
//or
var poster = doc.DocumentNode.SelectSingleNode("//div[@class='poster']//img")
?.Attributes["src"]?.Value;
}