c#爬虫爬取京东的商品信息

2020-01-05 09:57:08王旭

商品的图片地址和价格信息的获取需要仔细分析html中的数据,然后找到规律,比如价格在每个节点中就不能单独获取。

以下为批量获取价格的代码:


try
   {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("http://www.easck.com/prices/mgets?callback=jQuery1069298&type=1&area=1_72_4137_0&skuIds={0}&pdbp=0&pdtk=&pdpin=&pduid=1945966343&_=1469022843655", string.Join("%2C", productInfoList.Select(c => string.Format("J_{0}", c.ProductId))));
    string html = HttpHelper.DownloadUrl(sb.ToString());
    if (string.IsNullOrWhiteSpace(html))
    {
     return productInfoList;
    }
    html = html.Substring(html.IndexOf("(") + 1);
    html = html.Substring(0, html.LastIndexOf(")"));
    List<CommodityPrice> priceList = JsonConvert.DeserializeObject<List<CommodityPrice>>(html);
    productInfoList.ForEach(c => c.Price = priceList.FirstOrDefault(p => p.id.Equals(string.Format("J_{0}", c.ProductId))).p);
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.Message);
   }
   return productInfoList;

以上就是一个简单的爬取京东商品信息的爬虫,也可以根据自己的需求去解析更多的数据出来。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到c#教程频道。