HTMLAGILITY Pack을 사용하여 데이터를 스크랩하고 있지만 페이지가 제대로로드되지 않습니다.
내 코드는 페이지가 완전히로드 될 때까지 기다려야한다.
양식을 사용하여 브라우저를 사용하는 방법이 있지만 사용하지 않아도됩니다.
다음은 스크랩해야하는 링크 이며 다음은 내 코드입니다.
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"속성에있는 structured 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; }
}