python爬取淘宝销量_python爬取淘宝排名
import time
import json
import requests
import xlrd
import random
import os
from xlutils.copy import copy #導入模塊
excel = r‘C:\Users\Administrator\Desktop\查排名.xls‘
try:
rdx = xlrd.open_workbook(excel, formatting_info=True) #打開Excel,并保留原格式
except:
print( "no excel in %s " % excel )
newb = copy(rdx) #復制一份做輸出
#sh = rdx.sheet_by_index(name)
print(‘\033[31;1m已錄入型號 :\033[0m‘,(rdx.sheet_names())) #所有sheets.name
#sh=rdx.sheet_names()
for sheet_n in rdx.sheet_names(): #循環整個工作簿
sh1=rdx.sheet_by_name(sheet_n) #工作表對象
w_sheet=newb.get_sheet(sheet_n) #獲取sheet名稱查
id=str(int(sh1.cell_value(0,1))) #int浮點轉整數 str轉字符串
print(‘id‘ ,sh1.cell_value(0,1), id ,type(id))
print(‘\033[31;1m查詢型號 :\033[0m‘+ sheet_n)
tplt = "{:3}\t{:23}" #這里控制輸出 行數
for i in range(sh1.nrows): #非空行行數 整個型號的詞循環
i1=str(sh1.cell_value(i, 0))
if i1=="": #空行退出
break
time.sleep(random.uniform(1.5,3.6)) #隨機浮點數
url=‘https://s.m.taobao.com/search?q=‘+i1+‘&sst=1&n=20&buying=buyitnow&m=api4h5&token4h5=&abtest=3&wlsort=3&page=name‘
#url獲取地址1.淘寶手機端鏈接地址登錄s.m.taobao.com
2.輸入查詢的關鍵字 審查元素 這里記得刷新一次
3.network 右側name 一個個打開 需要的信息就在preview里面
4.恭喜找到自己需要的信息
# url=‘https://s.m.taobao.com/search?q=‘+i1+‘&sst=2&n=40&buying=buyitnow&m=api4h5&abtest=22&wlsort=22&page=name‘ #這里是找到目標真正的地址 headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.name; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36‘} comments = requests.get(url,headers=headers) #這里有問題臨時處理辦法 try: js = json.loads(comments.text) except json.decoder.JSONDecodeError: continue count=0 for j in js[‘listItem‘]: #所有店鋪信息 count+=1 if j[‘item_id‘]==id: #如果id相同 print(‘j]‘,j[‘item_id‘]==id) w_sheet.write(i,2,count) break else : # print(tplt.format(‘沒找到‘,i1)) w_sheet.write(i,2,‘沒找到‘) w_sheet.write(i+1,2,time.strftime(‘%m-%d ‘,time.gmtime())) print(" 任務完成!")os.remove(r‘C:\Users\Administrator\Desktop\查排名.xls‘)newb.save(r‘C:\Users\Administrator\Desktop\查排名.xls‘)print(‘\033[32;1m查詢完成!\033[0m‘,‘\n‘,‘warning:以上是查詢無線兩頁45位結果!‘)ps:記得Excel 不要有多于空白工作表 不然會報錯
總結
以上是生活随笔為你收集整理的python爬取淘宝销量_python爬取淘宝排名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Ajax调用WCF服务示
- 下一篇: SCAU华南农业大学-数电实验-用74L