gpt4 book ai didi

c# - 使用 C# HTMLAGILITYPACK 从网站抓取动态数据

转载 作者:行者123 更新时间:2023-11-30 16:40:11 28 4
gpt4 key购买 nike

我正在使用 HTMLAGILITY Pack 抓取数据,但页面加载不正确。

我需要我的代码应该等到页面完全加载。

有一些方法可以在表单中使用浏览器,但我不需要在表单中使用它。

这是 Link我需要抓取,下面是我的代码。

HtmlWeb web = new HtmlWeb();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HtmlAgilityPack.HtmlDocument doc = web.Load(website);
var goldTypes = doc.DocumentNode.SelectNodes("//h2[@class='gold-box-title']").ToList();
var goldPrices = doc.DocumentNode.SelectNodes("//span[@class='gold-box-price--sale'").ToList();

for (int i = 0; i < 2; i++)
{
string goldPrice = goldPrices[i].InnerText;
string goldType = goldTypes[i].InnerText;

}

最佳答案

你是对的,所有数据都在“buyable-gold”元素的“:buyable”属性中的结构化 json 中可用。

我做了一个快速测试,这应该是你想要的。这将为您提供包含所需数据的结构化对象列表。

HtmlWeb web = new HtmlWeb();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HtmlAgilityPack.HtmlDocument doc = web.Load("https://www.ezrsgold.com/buy-runescape-gold");

var buyGoldNodes = doc.DocumentNode.SelectNodes("//buyable-gold");

var buyableJsonList = buyGoldNodes.Select(x => HttpUtility.HtmlDecode(x.Attributes[":buyable"].Value)).ToList();

var buyables = buyableJsons.Select(x => JsonConvert.DeserializeObject<Buyable>(x)).ToList();

那么您的 Buyable 类将如下所示。

public class Buyable
{
public int id { get; set; }
public string sku { get; set; }
public int game_id { get; set; }
public string title { get; set; }
public int min_qty { get; set; }
public int max_qty { get; set; }
public string base_price { get; set; }
public string sale_price { get; set; }
public Bulk_Price[] bulk_price { get; set; }
public string delivery_time { get; set; }
public string description { get; set; }
public object sort_order { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string price { get; set; }
public bool on_sale { get; set; }
public int discount_from { get; set; }
}

public class Bulk_Price
{
public string qty { get; set; }
public string price { get; set; }
}

关于c# - 使用 C# HTMLAGILITYPACK 从网站抓取动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786423/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com