python爬取淘寶商品詳情頁數(shù)據(jù)
在講爬取淘寶詳情頁數(shù)據(jù)之前,先來介紹一款 Chrome 插件:Toggle JavaScript (它可以選擇讓網(wǎng)頁是否顯示 js 動態(tài)加載的內(nèi)容),如下圖所示:
當(dāng)這個插件處于關(guān)閉狀態(tài)時,待爬取的頁面顯示的數(shù)據(jù)如下:
當(dāng)這個插件處于打開狀態(tài)時,待爬取的頁面顯示的數(shù)據(jù)如下:
可以看到,頁面上很多數(shù)據(jù)都不顯示了,比如商品價格變成了劃線價格,而且累計評論也變成了0,說明這些數(shù)據(jù)都是動態(tài)加載的,以下演示真實價格的找法(評論內(nèi)容找法類似),首先檢查頁面元素,然后點擊Network選項卡,刷新頁面,可以看到很多動態(tài)加載的數(shù)據(jù),在里面找到包含商品價格的鏈接(可以使用Ctrl+f查找),如下圖所示:
將此鏈接在新的標(biāo)簽頁打開,如下圖所示,可以看到,被禁止訪問了,所以爬取的時候要在headers中加上Referer字段告訴服務(wù)器你是從哪個頁面鏈接過來的,Referer字段可以在這里查看:
評論數(shù)據(jù)的鏈接可以直接訪問(和價格信息找法類似),這里我知己去訪問它,如下圖所示:
可以看到評論信息總共有7頁,而且都是json格式的數(shù)據(jù),所以可以用json反序列化去抽取數(shù)據(jù),當(dāng)然也可以用正則表達(dá)式,下面我將演示用正則去抽取數(shù)據(jù),因為評論數(shù)據(jù)過多,這里只抓取第一頁,如果需要所有評論數(shù)據(jù),可以循環(huán)構(gòu)造url,只需要修改currentPage參數(shù)的值就行。程序源碼如下:
# filename:spider_taobao.py #!/usr/bin/env python # -*- coding=utf-8 -*- import re import urllib2 def spider_taobao(url): headers = { 'Accept':'application/json, text/plain, */*', 'Accept-Language':'zh-CN,zh;q=0.3', 'Referer':'https://item.taobao.com/item.htm', 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 'Connection':'keep-alive', } goods_id = re.findall('id=(\d+)', url)[0] try: req = urllib2.Request(url=url, headers=headers) res = urllib2.urlopen(req).read().decode('gbk', 'ignore') except Exception as e: print '無法打開網(wǎng)頁:', e.reason try: title = re.findall('<h3 class="tb-main-title" data-title="(.*?)"', res) title = title[0] if title else None line_price = re.findall('<em class="tb-rmb-num">(.*?)</em>', res)[0] # 30-42行為抓取淘寶商品真實價格,該數(shù)據(jù)是動態(tài)加載的 purl = "https://detailskip.taobao.com/service/getData/1/p1/item/detail/sib.htm?itemId={}&modules=price,xmpPromotion".format(goods_id) price_req = urllib2.Request(url=purl, headers=headers) price_res = urllib2.urlopen(price_req).read() data = list(set(re.findall('"price":"(.*?)"', price_res))) # data列表中的價格可能是定值與區(qū)間的組合,也可能只是定值,而且不一定有序 real_price = "" for t in data: if '-' in t: real_price = t break if not real_price: real_price = sorted(map(float, data))[0] # 45-53行為抓取評論數(shù)據(jù),該數(shù)據(jù)也是動態(tài)加載的 comment_url = "https://rate.tmall.com/list_detail_rate.htm?itemId={}&sellerId=880734502¤tPage=1".format(goods_id) comment_data = urllib2.urlopen(comment_url).read().decode("GBK", "ignore") temp_data = re.findall('("commentTime":.*?),"days"', comment_data) temp_data = temp_data if temp_data else re.findall('("rateContent":.*?),"reply"', comment_data) comment = "" for data in temp_data: comment += data.encode('utf-8') comment = comment if comment else "暫無評論" except Exception as e: print '數(shù)據(jù)抽取失敗!!!' print '商品名:', title print '劃線價格:', line_price print '真實價格:', real_price print '商品鏈接:', url print '部分評論內(nèi)容:', comment if __name__ == '__main__': #url = 'https://item.taobao.com/item.htm?spm=a230r.1.14.30.43306a3fOeuZ0B&id=553787375606&ns=1&abbucket=10#detail' url = raw_input("請輸入商品鏈接: ") spider_taobao(url)
運行結(jié)果如下:
更多內(nèi)容請參考專題《python爬取功能匯總》進行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python可視化單詞統(tǒng)計詞頻統(tǒng)計中文分詞的實現(xiàn)步驟
這篇文章主要介紹了Python可視化單詞統(tǒng)計詞頻統(tǒng)計中文分詞,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11Python安裝后測試連接MySQL數(shù)據(jù)庫方式
這篇文章主要介紹了Python安裝后測試連接MySQL數(shù)據(jù)庫方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07Streamlit+Echarts實現(xiàn)繪制精美圖表
在數(shù)據(jù)分析和可視化的領(lǐng)域,選擇合適的工具可以讓我們事半功倍,本文主要為大家介紹兩個工具,Streamlit和ECharts,感興趣的小伙伴可以跟隨小編一起了解下2023-09-09Python中的 ansible 動態(tài)Inventory 腳本
這篇文章主要介紹了Python中的 ansible 動態(tài)Inventory 腳本,本章節(jié)通過實例代碼從mysql數(shù)據(jù)作為數(shù)據(jù)源生成動態(tài)ansible主機為入口介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2020-01-01