-1

我正在尝试使用 AngleSharp 在 walmart.com 上抓取一些页面,但由于某种原因它无法正常工作。过去我一直在使用 AngleSharp 抓取许多网站,没有任何问题,但在这里它就不行了。

为简单起见,这里有一页:https ://www.walmart.com/ip/50908276 ,我正在尝试获取商品的价格(目前为 9.99 美元)。在 Chrome 的控制台中,当我键入时,document.getElementsByClassName("Price-characteristic")我会得到一个包含 60 个[span.Price-characteristic]结果的列表。完美的。但是,当我尝试使用 AngleBrackets 进行相同操作时,它不会返回任何内容。

这是我的代码:

using AngleSharp;
using AngleSharp.Dom;

public async void GetPrice()
{
    var config = Configuration.Default.WithDefaultLoader();
    string address = "https://www.walmart.com/ip/50908276";

    IDocument document = await
    BrowsingContext.New(config).OpenAsync(address);

    var priceDollar = document.GetElementsByClassName("Price-characteristic");
}

我对 HTML 不太熟悉,所以我为任何明显的无知道歉。

4

1 回答 1

1

使用HtmlAgilityPack和 XPath

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
    var html = await client.GetStringAsync("https://www.walmart.com/ip/50908276");
    var doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    var price = doc.DocumentNode
                    .SelectSingleNode("//*[@data-product-price]")
                    .Attributes["data-product-price"]
                    .Value;

}

此代码返回价格9.99

于 2017-01-03T20:40:51.070 回答