# //get_goods_from_taobao
成都創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、雨花臺(tái)網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、雨花臺(tái)網(wǎng)絡(luò)營(yíng)銷(xiāo)、雨花臺(tái)企業(yè)策劃、雨花臺(tái)品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供雨花臺(tái)建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):028-86922220,官方網(wǎng)址:aaarwkj.com
import requests
import re
import xlsxwriter
?
cok='' ?# 此處寫(xiě)入登錄之后自己的cookie
# 獲取頁(yè)面
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''
# ?格式化頁(yè)面,查找數(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('序號(hào)','價(jià)格','名稱(chēng)'))
????count=0
????for c in ilt:
????????count=count+1
????????print(tplt.format(count,c[0],c[1]))
# ?寫(xiě)入excel
def writetoexcel(list):
????print('開(kāi)始創(chuàng)建excel表格')
????book = xlsxwriter.Workbook(u'淘寶數(shù)據(jù).xlsx')
????sheet = book.add_worksheet()
????sheet.write(0, 0, '序號(hào)')
????sheet.write(0, 1, '名稱(chēng)')
????sheet.write(0, 2, '價(jià)格')
????row = 1
????col = 0
????for index, item in enumerate(list):
????????print('寫(xiě)入第%s行數(shù)據(jù)'%row)
????????sheet.write(row, col, index + 1) ?# 寫(xiě)入序號(hào)值
????????sheet.write(row, col + 1, item[1]) ?# 寫(xiě)入名稱(chēng)
????????sheet.write(row, col + 2, item[0]) ?# 寫(xiě)入價(jià)格
????????row += 1
????print('寫(xiě)入完成')
????book.close() ?# 關(guān)閉
?
?
def main():
????goods=input('請(qǐng)輸入想查詢(xún)的內(nèi)容:'.strip()) ?#輸入想搜索的商品名稱(chēng)
????function(){ //隔夜利息?http://www.kaifx.cn/question/kaifx/1752.html
????depth=3 ?# 爬取的頁(yè)數(shù)
????start_url='http://s.taobao.com/search?q='+goods ?# 搜索接口地址
????infoList=[]
????for i in range(depth):
????????try:
????????????page=i+1
????????????print('正在爬取第%s頁(yè)數(shù)據(jù)'%page)
????????????url=start_url+'&s='+str(44*i)
????????????html=getHTMLText(url)
????????????parsePage(infoList,html)
????????except:
????????????continue
????printGoodList(infoList)
????writetoexcel(infoList)
?
main()
名稱(chēng)欄目:pythoncookie獲取某寶商品信息
地址分享:http://aaarwkj.com/article16/gjojgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、域名注冊(cè)、企業(yè)建站、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)