# //get_goods_from_taobao
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、做網(wǎng)站、雨花臺網(wǎng)絡(luò)推廣、成都小程序開發(fā)、雨花臺網(wǎng)絡(luò)營銷、雨花臺企業(yè)策劃、雨花臺品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供雨花臺建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
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() ?# 關(guān)閉
?
?
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()