C# 爬蟲 爬取拼多多商品信息


根據拼多多搜索關鍵字爬取拼多多商品信息,如果沒有登錄,同一網絡爬取信息,最多可以爬取1~3次,你爬取之后你再次搜索就需要登錄,但有一個時間限制(這個沒有測試,估計1h后就會解封,就可以再次爬取),而且你切換網絡,也可以再次爬取,這個是沒有問題的。當然,你也可以的登錄之后爬取,這樣可以爬取N次,只有沒有被封號(但這是不可能的)

拼多多爬取商品信息,每次會返回   20條商品信息,他是放在一個HTML頁面,通過JS渲染上去的。就是這個API (https://mobile.yangkeduo.com/search_result.html?search_key=%E5%8D%8E%E4%B8%BA%E6%89%8B%E6%9C%BA&search_type=goods&source=index&options=3&search_met_track=manual&refer_page_el_sn=99884&refer_page_name=search_result&refer_page_id=10015_1615777503796_4wmnbjg2ck&refer_page_sn=10015)  
search_key 后面就是你的搜索的關鍵字。 還有 這個API的 get 請求。

 

 

代碼如下

public void GetShopList(string shopName){
  #region 獲取html
  string url = "https://mobile.yangkeduo.com/search_result.html?search_key=" + shopName+ "&search_type=goods&source=index&options=1&search_met_track=manual&refer_page_el_sn=99885&refer_page_name=search_result&refer_page_id=10015_1615453872480_bba38nr834&refer_page_sn=10015";
  RestRequest request = new RestRequest(url);
  request.Method = Method.GET;
  IRestResponse response = restClient.Execute(request);
  #endregion
  if (!response.IsSuccessful) return false;
  if (response.Content.Contains("window.rawData=null")) {
    Console.WriteLine("獲取失敗");     
    return false;
   }
   string shopList=response.Content
  analysis(shopList)
}
shoping 是商品關鍵字。
response.Content:就是商品的信息頁面,然后response.Content 后面的JS里面有個 window.rawData=,然后  window.rawData=null  說明這時你的ip暫時被封了如果是這樣的數據,說明數據獲取成功。

 
        
public void analysis(string shopList){
  #region 解析html 
  HtmlDocument htmlDocument = new HtmlDocument();
  htmlDocument.LoadHtml(content);
  var node1 = htmlDocument.DocumentNode.
  SelectNodes(@"//script");
  if (node1 is null) throw new Exception("Can not find id: __VIEWSTATE"); 
  #endregion
  foreach (var de in node1)
  {
   if (de.InnerHtml.Contains("window.rawData"))
   {
      string data = de.InnerText;
      data = System.Text.RegularExpressions.Regex.Split(data, "window.rawData=")[1];
      data = data.Substring(0, data.Length - 1);
      JsonData deJson = LitJson.JsonMapper.ToObject(data);
      deJson = deJson["store"]["listData"]["list"];                 
      for (int num_hisEQ = 0; num_hisEQ < deJson.Count; num_hisEQ++){
        string key=deJson[num_hisEQ]["key"].ToString();
        ………………
      }
    }
 }         
然后需要解析獲取的string,需要Nuget 里面下載  HtmlAgilityPack 和 LitJson  這兩個包。這樣就可以搞定了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM