使用C#HTMLAGILITYPACK从网站上刮取动态数据
我正在使用HTMLAGILITY Pack抓取数据,但页面加载不正确 我需要我的代码应该等到页面完全加载 在表单中使用浏览器有一些方法,但我不需要在表单中使用 这是我需要废弃的代码,下面是我的代码使用C#HTMLAGILITYPACK从网站上刮取动态数据,c#,web-scraping,html-agility-pack,C#,Web Scraping,Html Agility Pack,我正在使用HTMLAGILITY Pack抓取数据,但页面加载不正确 我需要我的代码应该等到页面完全加载 在表单中使用浏览器有一些方法,但我不需要在表单中使用 这是我需要废弃的代码,下面是我的代码 HtmlWeb web = new HtmlWeb(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HtmlAgilityPack.HtmlDocument doc =
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;
}
HtmlWeb=newhtmlweb();
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;
HtmlAgilityPack.HtmlDocument doc=web.Load(网站);
var goldTypes=doc.DocumentNode.SelectNodes(//h2[@class='gold-box-title'])。ToList();
var goldPrices=doc.DocumentNode.SelectNodes(//span[@class='gold-box-price--sale')).ToList();
对于(int i=0;i<2;i++)
{
字符串goldPrice=goldPrices[i].InnerText;
字符串goldType=goldTypes[i].InnerText;
}
您是正确的,所有数据都以结构化json的形式存在于“buyable gold”元素的“:buyable”属性中
我做了一个快速测试,这应该是您想要的。这将为您提供一个包含所需数据的结构化对象列表
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();
你的代码看起来不错。你有什么问题?@TheSoftwareEdi我得到的是变量名,而不是它们的值。该网站包含角度数据。感谢这项工作,但我不知道它是如何工作的?可购买黄金节点在哪里?我找不到它。你可以在查看源代码时找到它。只是检查它不会显示它。那么我如何找到它?只是查看页面源代码(右键单击->在chrome中查看页面源代码),然后像搜索任何东西一样搜索标记。是的-记住inspector在这么多JS修改后向您显示DOM。HtmlAgility从原始HTML源代码开始工作-这通常是不同的。
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; }
}