HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(item);
string title=null;
string cost= null;
var titleNode = doc.DocumentNode.SelectSingleNode("//span[contains(@class, 'title-info-title-text')]");
if (titleNode != null)
title = titleNode.InnerText;
var costNode = doc.DocumentNode.SelectSingleNode("//span[contains(@class, 'js-item-price')]");
if(costNode!=null)
cost = costNode.GetAttributeValue("content", "0");
这段代码在多个线程中执行,通常一切都很好。但有时 SelectSingleNode 返回 null。我设置了一个断点并手动检查。所需的标签是。感觉 doc.LoadHtml() - 异步工作,周期性地没有时间加载页面。还是我的内存快用完了?
也许有人遇到过这样的问题?
尝试首先加载 html,例如使用
WebClient,然后输入它HtmlDocument如果还是有问题,可以尝试循环下载几次。示例代码在这里