<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          教你用python爬取『京東』商品數(shù)據(jù),原來這么簡單!

          共 4300字,需瀏覽 9分鐘

           ·

          2021-03-14 08:43

          ↑ 關(guān)注 + 星標(biāo) ,每天學(xué)Python新技能

          后臺回復(fù)【大禮包】送你Python自學(xué)大禮包

          今日三條文章教大家如何學(xué)會分析『京東』商城商品數(shù)據(jù)。


          1

          前言


          本文將從小白的角度入手,一步一步教大家如何爬取『京東』商品數(shù)據(jù),文中以【筆記本】電腦為例!


          干貨內(nèi)容包括:

          1. 如何爬取商品信息?

          2. 如何爬取下一頁?

          3. 如何將爬取出來的內(nèi)容保存到excel?



          2

          分析網(wǎng)頁結(jié)構(gòu)


          1.查看網(wǎng)頁

          京東商城搜索框輸入:筆記本





          鏈接如下:

          https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page=9&s=241&click=1


          在瀏覽器里面按F12,分析網(wǎng)頁標(biāo)簽(這里我們需要爬取1.商品名稱2.商品價格、3.商品評論數(shù)


          2.分析網(wǎng)頁標(biāo)簽

          獲取當(dāng)前網(wǎng)頁所有商品




          可以看到在class標(biāo)簽id=J_goodsList里ul->li,對應(yīng)著所有商品列表


          獲取商品具體屬性


          每一個li(商品)標(biāo)簽中,class=p-name p-name-type-2對應(yīng)商品標(biāo)題,class=p-price對應(yīng)商品價格,class=p-commit對應(yīng)商品ID(方便后面獲取評論數(shù)


          避坑:

          這里商品評論數(shù)不能直接在網(wǎng)頁上獲取?。。?,需要根據(jù)商品ID去獲取。



          3

          爬取數(shù)據(jù)


          1.編程實現(xiàn)

          url="https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page=9&s=241&click=1"res = requests.get(url,headers=headers)res.encoding = 'utf-8'text = res.text

          selector = etree.HTML(text)list = selector.xpath('//*[@id="J_goodsList"]/ul/li')
          for i in list: title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0] price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0] product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","") print("title"+str(title))    print("price="+str(price))    print("product_id="+str(product_id)) print("-----")




          下面教大家如何獲取商品評論數(shù)!

          2.獲取商品評論數(shù)

          查看network,找到如下數(shù)據(jù)包




          將該url鏈接放到瀏覽器里面可以獲取到商品評論數(shù)



          分析url



          根據(jù)商品ID(可以同時多個ID一起獲?。┇@取商品評論數(shù)

          ?


          最后我們可以將獲取商品評論數(shù)的方法封裝成一個函數(shù)


          ###根據(jù)商品id獲取評論數(shù)def commentcount(product_id):    url = "https://club.jd.com/comment/productCommentSummaries.action?referenceIds="+str(product_id)+"&callback=jQuery8827474&_=1615298058081"    res = requests.get(url, headers=headers)    res.encoding = 'gbk'    text = (res.text).replace("jQuery8827474(","").replace(");","")    text = json.loads(text)    comment_count = text['CommentsCount'][0]['CommentCountStr']
          comment_count = comment_count.replace("+", "") ###對“萬”進(jìn)行操作 if "萬" in comment_count: comment_count = comment_count.replace("萬","") comment_count = str(int(comment_count)*10000)

          return comment_count


          此外,我們可以發(fā)現(xiàn)在獲取到的評論數(shù)包含“萬”“+”等符號,需要進(jìn)行相應(yīng)處理!


          for i in list:    title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0]    price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]    product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")            ###獲取商品評論數(shù)    comment_count = commentcount(product_id)    print("title"+str(title))    print("price="+str(price))    print("product_id="+str(comment_count))




          4

          保存到excel

          1.定義表頭

          import openpyxloutwb = openpyxl.Workbook()outws = outwb.create_sheet(index=0)
          outws.cell(row=1,column=1,value="index")outws.cell(row=1,column=2,value="title")outws.cell(row=1,column=3,value="price")outws.cell(row=1,column=4,value="CommentCount")


          引入openpyxl庫將數(shù)據(jù)保存到excel,表頭內(nèi)容包含(1.序號index、2.商品名稱title、3.商品價格price、4.評論數(shù)CommentCount


          2.開始寫入

              for i in list:        title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0]        price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]        product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")

          ###獲取商品評論數(shù) comment_count = commentcount(product_id) print("title"+str(title)) print("price="+str(price)) print("comment_count="+str(comment_count))
          outws.cell(row=count, column=1, value=str(count-1)) outws.cell(row=count, column=2, value=str(title)) outws.cell(row=count, column=3, value=str(price)) outws.cell(row=count, column=4, value=str(comment_count)) outwb.save("京東商品-李運(yùn)辰.xls")#保存


          最后保存成京東商品-李運(yùn)辰.xls




          5

          下一頁分析


          很重要!很重要!很重要!

          1.分析下一頁

          這里的下一頁與平常看到的不一樣,有點特殊!





          可以發(fā)現(xiàn)page和s有一下規(guī)律



          page以2遞增,s以60遞增。

          2.構(gòu)造下一頁鏈接

          遍歷每一頁def getpage():    page=1    s = 1    for i in range(1,6):        print("page="+str(page)+",s="+str(s))        url = "https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page="+str(page)+"&s="+str(s)+"&click=1"        getlist(url)        page = page+2        s = s+60


          這樣就可以爬取下一頁。


          6

          總結(jié)


          1.入門爬蟲(京東商品數(shù)據(jù)為例)。

          2.如何獲取網(wǎng)頁標(biāo)簽。

          3.獲取『京東』商品評論數(shù)

          4.如何通過python將數(shù)據(jù)保存到excel

          5.分析構(gòu)造『京東』商品網(wǎng)頁下一頁鏈接


          如果大家對本文代碼源碼感興趣,點擊下放名片關(guān)注后,回復(fù):京東商品 ,獲取完整代碼!



          如果大家想加群學(xué)習(xí),后臺點擊:加群交流


          推薦閱讀

          1. 工程師姓什么很重要!別再叫我“X工”?。。?/a>

          2. 這可能是你近2年進(jìn)騰訊的最好機(jī)會!

          3. 穩(wěn)了!已官宣:抄小道進(jìn)騰訊的機(jī)會來了

          掃碼回復(fù)「大禮包」后獲取大禮

          瀏覽 134
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  性爱视频网站久久 | 在线水蜜桃 | 北条麻妃一区三区 | 天天操妞网 | 无码免费婬AV片在线观看 |