日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 爬虫基础——淘宝评论

發布時間:2023/12/9 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬虫基础——淘宝评论 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

描述:

本文主要利用re模塊簡單的對淘寶爬蟲進行介紹,簡單的爬蟲入門,便于理解,初學者可做參考,復雜的后續會不間斷的更新。

目標:

1.學會使用re模塊

2.對目標物品的評論進行抓取;

3.將抓取到的內容分析并保存到數據庫。

過程:

1.首先設置返回的文本格式(非常重要)

# coding:utf-8

2.導入爬蟲過程所需的包(這里需用到requests 和 re 模塊)

? ?requests庫是一個功能很強大的網絡請求庫,可以實現和瀏覽器一樣發送各種HTTP請求來獲取網站的數據。

?Re庫是Python的標準庫,主要用于字符串匹配。(具體使用方法建議參考:?https://blog.csdn.net/i_chaoren/article/details/62264414)

? ?pymysql庫是python連接到MySQL數據庫的接口。

# coding:utf-8 import requests import re import pymysql

3.創建循環鏈接

注意:淘寶評論有一個單獨的隱藏頁面,并不能在商品詳情頁上直接獲得淘寶評論。

這里給出一個簡單的淘寶評論鏈接獲取的方式(以麗水山耕旗艦店下的蘿卜泡菜為例):

1.經觀察淘寶評論頁面的鏈接如下形式: https://rate.tmall.com/list_detail_rate.htm? itemId=?&spuId=345965243&sellerId=?&order=1&currentPage=%s

2.我們每次僅需要改變鏈接中的itemId和sellerId即可 獲得所需鏈接,那么如何獲得itemId和sellerId呢?

首先,蘿卜泡菜的鏈接為:https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-16954807471.50.4df126e3aGs0H1&id=552781332928&rn=871c9d5cb9aa8e6fe37224f0e77b7042&abbucket=5

其中d為淘寶評論鏈接里的sellerId,即itemId=552781332928

其次,通過娃娃魚進入娃娃魚所在店鋪,得麗水山耕旗艦店的鏈接為:https://lishuishangeng.tmall.com/shop/view_shop.htm?spm=a230r.1.14.10.446f1ddaFQlxGA&user_number_id=3252187135

其中user_number_id為淘寶評論鏈接里的sellerId,即sellerId=3252187135

最后current Page即淘寶評論所在的頁數

故最終得麗水山耕蘿卜泡菜評論所在的鏈接為:https://rate.tmall.com/list_detail_rate.htm? itemId=552781332928&spuId=345965243&sellerId=3252187135&order=1&currentPage=i,其中i=1,2,3.....

淘寶評論頁面如圖所示:

3.創建循環鏈接(因為淘寶評論有時候不止一頁,這里以100頁為例)。

urls = [] for i in list(range(1,100)):urls.append('https://rate.tmall.com/list_detail_rate.htm?itemId=552781332928&spuId=345965243&sellerId=3252187135&order=1&currentPage=%s' %i)

4.循環遍歷淘寶評論鏈接,獲取所需信息。 (此部分需用到正則表達式)

注:

? ? name:評論者姓名

? ? time:評論時間

? ? ratecontent:評論內容

# 構建字段容器 name = [] time = [] ratecontent = []# 循環抓取數據 for url in urls:content = requests.get(url).text# 借助正則表達式使用findall進行匹配查詢name.extend(re.findall(re.compile('"displayUserNick":"(.*?)","displayUserNumId"'),content))ratecontent.extend(re.findall(re.compile('"rateContent":"(.*?)","rateDate"'), content))time.extend(re.findall(re.compile('"rateDate":"(.*?)","reply"'),content))

?5.將數據傳入數據庫

#鏈接數據庫 con = pymysql.connect('localhost', 'root', 'zn025425', 'lishuisg',charset='utf8') #創建游標 cur = con.cursor() #執行sql,在MySQL中創建表taobao cur.execute("CREATE TABLE TAOBAO(NAME VARCHAR(30),TIME VARCHAR(30),RATECONTENT VARCHAR(100))") #循環遍歷插入數據 for i in range(len(name)):sql = "insert into taobao VALUES(%s,%s,%s)"cur.execute(sql,(name[i],time[i],ratecontent[i]))#保存新建或更新的數據con.commit() #關閉游標 cur.close() #關閉鏈接 con.close()

6.完整代碼如下:

# coding:utf-8 # 導入所需的開發模塊 import requests import re import pymysql# 創建循環鏈接 urls = [] for i in list(range(1,100)):urls.append('https://rate.tmall.com/list_detail_rate.htm?itemId=552781332928&spuId=345965243&sellerId=3252187135&order=1&currentPage=%s' %i)# 構建字段容器 name = [] time = [] ratecontent = []# 循環抓取數據 for url in urls:content = requests.get(url).text# 借助正則表達式使用findall進行匹配查詢name.extend(re.findall(re.compile('"displayUserNick":"(.*?)","displayUserNumId"'),content))ratecontent.extend(re.findall(re.compile('"rateContent":"(.*?)","rateDate"'), content))time.extend(re.findall(re.compile('"rateDate":"(.*?)","reply"'),content))con = pymysql.connect('localhost', 'root', 'zn025425', 'lishuisg',charset='utf8') cur = con.cursor() #cur.execute("CREATE TABLE TAOBAO(NAME VARCHAR(30),TIME VARCHAR(30),RATECONTENT VARCHAR(100))") for i in range(len(name)):sql = "insert into taobao VALUES(%s,%s,%s)"cur.execute(sql,(name[i],time[i],ratecontent[i]))con.commit() cur.close() con.close()

7.執行結果如下:

?

?

總結

以上是生活随笔為你收集整理的python 爬虫基础——淘宝评论的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。