简体   繁体   English

HtmlAgilityPack 中的 SelectNodes 总是返回 null

[英]SelectNodes in HtmlAgilityPack always return null

I'm trying to get data from this site: https://www.hltv.org/results , and it doesnt work.我正在尝试从该站点获取数据: https://www.hltv.org/results ,但它不起作用。 SelectNodes always returns null, I've tried using xpath, full xpath and so on and different things other than HtmlAgilityPack. SelectNodes 总是返回 null,我尝试过使用 xpath、完整的 xpath 等等以及 HtmlAgilityPack 以外的其他东西。 Maybe its not the HtmlAgilityPack maybe the problem is in the tags.也许它不是 HtmlAgilityPack,也许问题出在标签上。 Please take a look at the code and view also the tags to see if if i copied it right:请查看代码并查看标签,看看我是否复制正确:

    HtmlWeb web = new HtmlWeb();
    var doc = web.Load("https://www.hltv.org/results");
    var teams = doc.DocumentNode.SelectNodes("//*[@class='team team-won']");

Please Help, Thanks!请帮忙,谢谢!

HtmlAgilityPack does not handle javascript, so you can use selenium to store the html source code via webDriver. HtmlAgilityPack 不处理 javascript,因此您可以使用 selenium 通过 webDriver 存储 html 源代码。

IWebDriver webDriver = new ChromeDriver();

webDriver.Url = "https://www.hltv.org/results";
var pageSource = webDriver.PageSource;

var doc = new HtmlDocument();
doc.LoadHtml(pageSource);

 var xPath = @"//*[@class='team team-won']";

 var node = doc.DocumentNode.SelectSingleNode(xPath);

 Console.WriteLine(node.InnerText);

Make sure that you have already downloaded and stored in same directory the webDriver.(i used chrome driver downloaded from here https://chromedriver.chromium.org/downloads )确保您已经下载并存储在同一目录中的 webDriver。(我使用从这里下载的 chrome 驱动程序https://chromedriver.chromium.org/downloads

Use load for only files.仅对文件使用加载。 In this example, you ar loading an url, So you should use web.LoahHtml İf you do this.在这个例子中,你正在加载一个 url,所以你应该使用 web.LoahHtml 如果你这样做。 It will work它会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM