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

时间:2018-08-10 12:20:54

标签: c# web-scraping html-agility-pack

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

我需要代码等待页面完全加载。

可以使用某种形式的浏览器来处理表单,但是我不需要在表单中使用它。

这是我需要剪贴的Link,下面是我的代码。

package com.x.dao;
import com.x.model.Emp;

@Component
public class EmpDao {

public void saveEmpObj(Emp emp) {
    System.out.println(emp.getEname());
          System.out.println("===============>Emp Dao Called<==================");
}
}

1 个答案:

答案 0 :(得分:2)

您是正确的,所有数据都可以在“ 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; }
}