Python爬蟲實(shí)戰(zhàn),requests+openpyxl模塊,爬取手機(jī)商品信息數(shù)據(jù)(附源碼)

前言

今天給大家介紹的是Python爬取手機(jī)商品信息數(shù)據(jù),在這里給需要的小伙伴們代碼,并且給出一點(diǎn)小心得。

首先是爬取之前應(yīng)該盡可能偽裝成瀏覽器而不被識(shí)別出來是爬蟲,基本的是加請求頭,但是這樣的純文本數(shù)據(jù)爬取的人會(huì)很多,所以我們需要考慮更換代理IP和隨機(jī)更換請求頭的方式來對手機(jī)信息數(shù)據(jù)進(jìn)行爬取。

在每次進(jìn)行爬蟲代碼的編寫之前,我們的第一步也是最重要的一步就是分析我們的網(wǎng)頁。

通過分析我們發(fā)現(xiàn)在爬取過程中速度比較慢,所以我們還可以通過禁用谷歌瀏覽器圖片、JavaScript等方式提升爬蟲爬取速度。

手機(jī)

開發(fā)工具

Python版本: 3.6

相關(guān)模塊:

requests模塊

json模塊

lxml模塊

openpyxl

環(huán)境搭建

安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。

文中完整代碼及Excel文件,評(píng)論留言獲取

思路分析

瀏覽器中打開我們要爬取的頁面
按F12進(jìn)入開發(fā)者工具,查看我們想要的手機(jī)商品數(shù)據(jù)在哪里
這里我們需要頁面數(shù)據(jù)就可以了

源代碼結(jié)構(gòu)

代碼實(shí)現(xiàn)

請求頭防止反爬

# 這里提示不用請求也是可以的只保留user-agent也可以爬取數(shù)據(jù)
headers = {
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.
            100 Safari/537.36',
            'cookie':'你的Cookie',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'upgrade-insecure-requests': '1',
            'referer': 'https://www.jd.com/',
        }

獲取商品評(píng)論數(shù)

import openpyxl
outwb = 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")

count=2

根據(jù)商品id獲取評(píng)論數(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

獲取每一頁的商品數(shù)據(jù)

def getlist(url):
    global  count
    #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_","")

        comment_count = commentcount(product_id)
        #print(title)
        #print(price)
        #print(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))

        count = count +1
        #print("-----")

遍歷每一頁

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=手機(jī)=utf-8&wq=手機(jī)=56b2bc7c47db4861986201bb72c1b281"+str(page)+"&s="+str(s)+"&click=1"
        getlist(url)
        page = page+2
        s = s+60

結(jié)果展示

數(shù)據(jù)結(jié)果

最后

今天的分享到這里就結(jié)束了 ,感興趣的朋友也可以去試試哈

對文章有問題的,或者有其他關(guān)于python的問題,可以在評(píng)論區(qū)留言或者私信我哦

覺得我分享的文章不錯(cuò)的話,可以關(guān)注一下我,或者給文章點(diǎn)贊(/≧▽≦)/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容