Python爬蟲實戰,requests+openpyxl模塊,爬取手機商品信息數據(附源碼)

前言

今天給大家介紹的是Python爬取手機商品信息數據,在這裏給需要的小夥伴們代碼,並且給出一點小心得。

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

在每次進行爬蟲代碼的編寫之前,我們的第一步也是最重要的一步就是分析我們的網頁。

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

開發工具

Python版本: 3.6

相關模塊:

requests模塊

json模塊

lxml模塊

openpyxl

環境搭建

安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。

文中完整代碼及Excel文件,評論留言獲取

思路分析

瀏覽器中打開我們要爬取的頁面
按F12進入開發者工具,查看我們想要的手機商品數據在哪裏
這裏我們需要頁面數據就可以了

代碼實現

請求頭防止反爬

# 這裏提示不用請求也是可以的只保留user-agent也可以爬取數據
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/',
        }

獲取商品評論數

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

根據商品id獲取評論數

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("+", "")
    ###對“萬”進行操作
    if "萬" in comment_count:
        comment_count = comment_count.replace("萬","")
        comment_count = str(int(comment_count)*10000)

    return comment_count

獲取每一頁的商品數據

def getlist(url):
    global  count
    #url="https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯想%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=手機=utf-8&wq=手機=56b2bc7c47db4861986201bb72c1b281"+str(page)+"&s="+str(s)+"&click=1"
        getlist(url)
        page = page+2
        s = s+60

結果展示

最後

今天的分享到這裏就結束了 ,感興趣的朋友也可以去試試哈

對文章有問題的,或者有其他關於python的問題,可以在評論區留言或者私信我哦

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章