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

首頁(yè) > 編程 > Python > 正文

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

2020-02-22 23:17:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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

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

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

 

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

  可以看到評(píng)論信息總共有7頁(yè),而且都是json格式的數(shù)據(jù),所以可以用json反序列化去抽取數(shù)據(jù),當(dāng)然也可以用正則表達(dá)式,下面我將演示用正則去抽取數(shù)據(jù),因?yàn)樵u(píng)論數(shù)據(jù)過(guò)多,這里只抓取第一頁(yè),如果需要所有評(píng)論數(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ú)法打開(kāi)網(wǎng)頁(yè):', 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í)價(jià)格,該數(shù)據(jù)是動(dòng)態(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列表中的價(jià)格可能是定值與區(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行為抓取評(píng)論數(shù)據(jù),該數(shù)據(jù)也是動(dòng)態(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 "暫無(wú)評(píng)論" except Exception as e:  print '數(shù)據(jù)抽取失敗!!!' print '商品名:', title print '劃線價(jià)格:', line_price print '真實(shí)價(jià)格:', real_price print '商品鏈接:', url print '部分評(píng)論內(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("請(qǐng)輸入商品鏈接: ") spider_taobao(url)            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄯善县| 唐河县| 宜都市| 饶阳县| 綦江县| 贵南县| 水富县| 定州市| 土默特右旗| 巩义市| 永寿县| 栖霞市| 白水县| 沐川县| 灵川县| 定南县| 法库县| 涿鹿县| 大埔区| 琼结县| 东台市| 马龙县| 秭归县| 石家庄市| 佛坪县| 泸水县| 汾西县| 湟源县| 资阳市| 上饶县| 四子王旗| 乐平市| 姜堰市| 济源市| 天祝| 苏尼特左旗| 乐山市| 冷水江市| 元朗区| 从江县| 赤城县|