我正在解析 HTML 代码以在“Windows 商店应用程序”中获取图像链接,我正在使用 Html Agility Pack!这里的代码:
async void LoidContent()
{
foreach (var feedItem in feedData.Items)
{
HttpClient httpClient = new HttpClient();
var stream = await httpClient.GetStreamAsync(feedItem.Link);
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.Load(stream);
// GET IMAGE
var div = htmlDoc.DocumentNode.Descendants().FirstOrDefault(
d =>
d.Name == "div" && d.Attributes["class"] != null && d.Attributes["class"].Value == "pika-stage img");
var img = div.Descendants("img").FirstOrDefault();
if (img != null)
{
string imgLinks = img.Attributes["src"].Value;
feedItem.Image = new Uri(imgLinks);
}
}}
有时应用程序会在此行中崩溃“对象引用未设置为对象的实例”
var img = div.Descendants("img").FirstOrDefault();