溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python cookie 獲取某寶商品信息

發(fā)布時間:2020-08-01 10:10:13 來源:網(wǎng)絡 閱讀:191 作者:專注地一哥 欄目:編程語言

# //get_goods_from_taobao

import requests

import re

import xlsxwriter

?

cok='' ?# 此處寫入登錄之后自己的cookie

# 獲取頁面

def getHTMLText(url):

????headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}

????usercookies=cok

????cookies={}

????for a in usercookies.split(';'):

????????name,value=a.strip().split('=',1)

????????cookies[name]=value

????try:

????????r=requests.get(url,cookies=cookies,headers=headers,timeout=60)

????????r.raise_for_status()

????????r.encoding=r.apparent_encoding

????????return r.text

????except:

????????return''

# ?格式化頁面,查找數(shù)據(jù)

def parsePage(ilt,html):

????try:

????????print('爬取成功')

????????plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)

????????tlt=re.findall(r'\"raw_title\"\:\".*?\"',html)

????????for i in range(len(plt)):

????????????price=eval(plt[i].split(':')[1])

????????????title=eval(tlt[i].split(':')[1])

????????????ilt.append([price,title])

????except:

????????print('')

# ?打印數(shù)據(jù)列表

def printGoodList(ilt):

????tplt='{:4}\t{:8}\t{:16}'

????print(tplt.format('序號','價格','名稱'))

????count=0

????for c in ilt:

????????count=count+1

????????print(tplt.format(count,c[0],c[1]))

# ?寫入excel

def writetoexcel(list):

????print('開始創(chuàng)建excel表格')

????book = xlsxwriter.Workbook(u'淘寶數(shù)據(jù).xlsx')

????sheet = book.add_worksheet()

????sheet.write(0, 0, '序號')

????sheet.write(0, 1, '名稱')

????sheet.write(0, 2, '價格')

????row = 1

????col = 0

????for index, item in enumerate(list):

????????print('寫入第%s行數(shù)據(jù)'%row)

????????sheet.write(row, col, index + 1) ?# 寫入序號值

????????sheet.write(row, col + 1, item[1]) ?# 寫入名稱

????????sheet.write(row, col + 2, item[0]) ?# 寫入價格

????????row += 1

????print('寫入完成')

????book.close() ?# 關閉

?

?

def main():

????goods=input('請輸入想查詢的內(nèi)容:'.strip()) ?# 輸入想搜索的商品名稱

????function(){ //隔夜利息?http://www.kaifx.cn/question/kaifx/1752.html

????depth=3 ?# 爬取的頁數(shù)

????start_url='http://s.taobao.com/search?q='+goods ?# 搜索接口地址

????infoList=[]

????for i in range(depth):

????????try:

????????????page=i+1

????????????print('正在爬取第%s頁數(shù)據(jù)'%page)

????????????url=start_url+'&s='+str(44*i)

????????????html=getHTMLText(url)

????????????parsePage(infoList,html)

????????except:

????????????continue

????printGoodList(infoList)

????writetoexcel(infoList)

?

main()


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI