我正在尝试从网页中的特定div获取图像标记。这是网页链接page link
我使用过这段代码:
var webGet = new HtmlWeb();
var document = webGet.Load(txt.Text);
var infos = from info in document.DocumentNode.SelectNodes("//div[@id='custom-description']")
from link in info.SelectNodes("img").Where(x => x.Attributes.Contains("src"))
select new
{
LinkURL = link.Attributes["src"].Value
};
lbl.Text = infos.ToString();
但它返回null值。 请告诉我这段代码有什么不对。 提前致谢
答案 0 :(得分:0)
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument document = web.Load(url);
var rateNode = from info in document.DocumentNode.SelectNodes("//div[@class='class name']")
from link in info.SelectNodes("//img").Where(x=>x.Attributes.Contains("src"))
select new
{
link.Attributes["src"].Value
};
// return View(lstRecords);
string result;
lbl.Text = rateNode.ToString();
foreach (var a in rateNode)
{
int count=0;
Image img = new Image();
img.ID = count + "a";
count++;
img.ImageUrl = a.Value
Controls.Add(img);
}
答案 1 :(得分:0)
你在这里写的Linq查询看起来还不错。但这里的问题是id为custom-description
的div不包含img
元素。因此查询返回null结果。