溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-06-11 17:14:52 來源:億速云 閱讀:186 作者:Leah 欄目:編程語言

使用c#怎么爬取京東商品信息,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

一、下載網(wǎng)頁HTML

首先我們寫一個公共方法用來下載網(wǎng)頁的HTML。

在寫下載HTML方法之前,我們需要去查看京東網(wǎng)頁請求頭的相關信息,在發(fā)送請求時需要用到。

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();//讀取數(shù)據(jù)
   sr.Close();
  }
  catch (Exception ex)
  {
   html = null;
  }
  }
 }
 }
 catch (System.Net.WebException ex)
 {
  html = null;
 }
 catch (Exception ex)
 {
 html = null;
 }
 return html;
}

如上代碼所示,我們使用WebRequest來獲取網(wǎng)頁信息,在發(fā)送請求之前,需要先設置和京東頁面一樣的請求頭。

以上設置的信息比較簡單,但能夠正常發(fā)送請求,我們也可以模擬瀏覽器設置cookie等等信息,

二、解析HTML

獲取所有商品的信息分為兩個步驟

    (1)根據(jù)商品分類頁面獲取所有商品分類的URL

    (2)根據(jù)商品分類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)選擇節(jié)點,獲取每個大類的節(jié)點集合

      (3)根據(jù)每個大類的節(jié)點,獲取每個小類的節(jié)點信息,然后獲取到分類地址

節(jié)點中也包含了其它很多信息,可以根據(jù)自己的需求去獲取對應的信息

2、獲取具體商品信息

(1)首先根據(jù)商品分類加載分類信息,獲取到當前分類每個頁面的鏈接

下載HTML之后,選擇節(jié)點,可以將HTML格式化之后查看每個頁面的url地址和拼接規(guī)則,然后借助HtmlAgilityPack

來篩選需要的節(jié)點,將每個頁面的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)根據(jù)每個頁面的鏈接,獲取當前頁面的商品信息

下載每個頁面的所有商品信息,需要獲取的商品信息在頁面中都能找到

首先我們獲取到每個商品的節(jié)點集合,獲取到一個商品的節(jié)點信息之后,分析html數(shù)據(jù),

找到我們需要的商品的信息所在的位置,然后將需要的信息分離出來。

下面的代碼中我獲取到的商品的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中的數(shù)據(jù),然后找到規(guī)律,比如價格在每個節(jié)點中就不能單獨獲取。

以下為批量獲取價格的代碼:

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;

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI