国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python爬取淘寶商品詳情頁數(shù)據(jù)

2020-01-04 15:48:12
字體:
供稿:網(wǎng)友

在講爬取淘寶詳情頁數(shù)據(jù)之前,先來介紹一款 Chrome 插件:Toggle JavaScript (它可以選擇讓網(wǎng)頁是否顯示 js 動態(tài)加載的內(nèi)容),如下圖所示:

python,爬取,淘寶,商品數(shù)據(jù)

當這個插件處于關(guān)閉狀態(tài)時,待爬取的頁面顯示的數(shù)據(jù)如下:

python,爬取,淘寶,商品數(shù)據(jù)

當這個插件處于打開狀態(tài)時,待爬取的頁面顯示的數(shù)據(jù)如下:

python,爬取,淘寶,商品數(shù)據(jù)

  可以看到,頁面上很多數(shù)據(jù)都不顯示了,比如商品價格變成了劃線價格,而且累計評論也變成了0,說明這些數(shù)據(jù)都是動態(tài)加載的,以下演示真實價格的找法(評論內(nèi)容找法類似),首先檢查頁面元素,然后點擊Network選項卡,刷新頁面,可以看到很多動態(tài)加載的數(shù)據(jù),在里面找到包含商品價格的鏈接(可以使用Ctrl+f查找),如下圖所示:

python,爬取,淘寶,商品數(shù)據(jù)

  將此鏈接在新的標簽頁打開,如下圖所示,可以看到,被禁止訪問了,所以爬取的時候要在headers中加上Referer字段告訴服務(wù)器你是從哪個頁面鏈接過來的,Referer字段可以在這里查看:

python,爬取,淘寶,商品數(shù)據(jù)

評論數(shù)據(jù)的鏈接可以直接訪問(和價格信息找法類似),這里我知己去訪問它,如下圖所示:

python,爬取,淘寶,商品數(shù)據(jù)

  可以看到評論信息總共有7頁,而且都是json格式的數(shù)據(jù),所以可以用json反序列化去抽取數(shù)據(jù),當然也可以用正則表達式,下面我將演示用正則去抽取數(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 reimport urllib2def 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:   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)容:', commentif __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é)果如下:

python,爬取,淘寶,商品數(shù)據(jù)

 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 博爱县| 利辛县| 信阳市| 公安县| 仁寿县| 依安县| 云浮市| 高邑县| 兴化市| 乌兰察布市| 铅山县| 邓州市| 平远县| 邮箱| 都安| 揭东县| 武川县| 淳化县| 菏泽市| 阳高县| 贵定县| 永春县| 湟中县| 赤峰市| 辉南县| 松滋市| 平罗县| 广南县| 武胜县| 崇礼县| 仁布县| 甘孜县| 商河县| 彭阳县| 万盛区| 沁源县| 古交市| 鸡西市| 寿阳县| 东乡族自治县| 修水县|