温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用c#怎么爬取京东商品信息

发布时间:2021-06-11 17:14:52 来源:亿速云 阅读:161 作者:Leah 栏目:编程语言

使用c#怎么爬取京东商品信息,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一、下载网页HTML

首先我们写一个公共方法用来下载网页的HTML。

在写下载HTML方法之前,我们需要去查看京东网页请求头的相关信息,在发送请求时需要用到。

public static string DownloadHtml(string url, Encoding encode)
{
 string html = string.Empty;
 try
 {
 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 request.Timeout = 30 * 1000;
 request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
 request.ContentType = "text/html; charset=utf-8";
 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 {
  if (response.StatusCode == HttpStatusCode.OK)
  {
  try
  {
   StreamReader sr = new StreamReader(response.GetResponseStream(), encode);
   html = sr.ReadToEnd();//读取数据
   sr.Close();
  }
  catch (Exception ex)
  {
   html = null;
  }
  }
 }
 }
 catch (System.Net.WebException ex)
 {
  html = null;
 }
 catch (Exception ex)
 {
 html = null;
 }
 return html;
}

如上代码所示,我们使用WebRequest来获取网页信息,在发送请求之前,需要先设置和京东页面一样的请求头。

以上设置的信息比较简单,但能够正常发送请求,我们也可以模拟浏览器设置cookie等等信息,

二、解析HTML

获取所有商品的信息分为两个步骤

    (1)根据商品分类页面获取所有商品分类的URL

    (2)根据商品分类URL获取每个商品

 1、获取商品分类

try
{
 string html = HttpHelper.DownloadUrl(@"http://www.jd.com/allSort.aspx");
 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(html);
 string goodClass= @"//*[@class='items']/dl/dd";
 HtmlNodeCollection noneNodeList = doc.DocumentNode.SelectNodes(goodClass);
 foreach (var node in noneNodeList)
 {
 HtmlDocument docChild = new HtmlDocument();
 docChild.LoadHtml(node.OuterHtml);
 string urlPath = "/dd/a";
 HtmlNodeCollection list = docChild.DocumentNode.SelectNodes(urlPath);
 foreach (var l in list)
 {
  HtmlDocument docChild1 = new HtmlDocument();
  docChild1.LoadHtml(l.OuterHtml);
  var sortUrl = l.Attributes["href"].Value;
  if (!string.IsNullOrWhiteSpace(sortUrl) && sortUrl.Contains("cat="))
  {
  InsertSort("https:" + sortUrl);
  }
 }
 }
}
catch (Exception ex)
{
 Console.WriteLine(ex.Message);
}

上面的代码中使用到了HtmlAgilityPack来解析HTML信息,这是.NET的开源项目,开源在nuget包中下载。

      (1)下载http://www.jd.com/allSort.aspx的html页,然后加载到HtmlDocument

      (2)选择节点,获取每个大类的节点集合

      (3)根据每个大类的节点,获取每个小类的节点信息,然后获取到分类地址

节点中也包含了其它很多信息,可以根据自己的需求去获取对应的信息

2、获取具体商品信息

(1)首先根据商品分类加载分类信息,获取到当前分类每个页面的链接

下载HTML之后,选择节点,可以将HTML格式化之后查看每个页面的url地址和拼接规则,然后借助HtmlAgilityPack

来筛选需要的节点,将每个页面的url分离出来

try
{
 string html = HttpHelper.DownloadUrl(@"https://list.jd.com/list.html?cat=1620,11158,11964");
 HtmlDocument productDoc = new HtmlDocument();
 productDoc.LoadHtml(html);
 HtmlNode pageNode = productDoc.DocumentNode.SelectSingleNode(@"//*[@id='J_topPage']/span/i");
 if (pageNode != null)
 {
  int pageNum = Convert.ToInt32(pageNode.InnerText);
  for (int i = 1; i < pageNum + 1; i++)
  {
   string pageUrl = string.Format("{0}&page={1}", category.Url, i).Replace("&page=1&", string.Format("&page={0}&", i));
   try
   {
    List<ProductInfo> proDuctInfo = GetPageProduct(pageUrl);
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.Message);
   }
  }
 }
 
}
catch (Exception ex)
{
 Console.WriteLine(ex.Message);
}

(2)根据每个页面的链接,获取当前页面的商品信息

下载每个页面的所有商品信息,需要获取的商品信息在页面中都能找到

首先我们获取到每个商品的节点集合,获取到一个商品的节点信息之后,分析html数据,

找到我们需要的商品的信息所在的位置,然后将需要的信息分离出来。

下面的代码中我获取到的商品的id和title还有价格。

List<ProductInfo> productInfoList = new List<ProductInfo>();
try
{
 string html = HttpHelper.DownloadUrl(url);
 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(html);
 HtmlNodeCollection productNodeList = doc.DocumentNode.SelectNodes("//*[@id='plist']/ul/li");
 if (productNodeList == null || productNodeList.Count == 0)
 {
  return productInfoList;
 }
 foreach (var node in productNodeList)
 {
  HtmlDocument docChild = new HtmlDocument();
  docChild.LoadHtml(node.OuterHtml);
  ProductInfo productInfo = new ProductInfo()
  {
   CategoryId = category.Id
  };
 
  HtmlNode urlNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-name']/a");
  if (urlNode == null)
  {
   continue;
  }
  string newUrl= urlNode.Attributes["href"].Value;
  newUrl = !newUrl.StartsWith("http:")?"http:" + newUrl: newUrl;
  string sId = Path.GetFileName(newUrl).Replace(".html", "");
  productInfo.ProductId = long.Parse(sId);
  HtmlNode titleNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-name']/a/em");
  if (titleNode == null)
  {
   continue;
  }
  productInfo.Title = titleNode.InnerText;
  
  HtmlNode priceNode = docChild.DocumentNode.SelectSingleNode("//*[@class='p-price']/strong/i");
  if (priceNode == null)
  {
   continue;
  }
  else
  {
 
  }
  productInfoList.Add(productInfo);
  
 }
 //批量获取价格
 GetGoodsPrice(productInfoList);
}
catch (Exception ex)
{
}
return productInfoList;

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

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

try
   {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("http://p.3.cn/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;

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI