|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
import re
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'cookie': 't=35a6a41e2ab33d67331fef54fa6b7d96; thw=cn; enc=Bq%2B8Hihnf5qMksZKyDHuPexyE%2FWAYRLtDhlzA7nKXPktlHXnojEcfOP3XKrS3v6NpJFVbSC%2BYyrqArSYFIxGPA%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; _fbp=fb.1.1570716221972.1308016870; cna=YUvuFeTOdF0CAQHB/IooKSrp; tracknick=tb_3807958; tg=0; UM_distinctid=16de352a97e319-045b9f9e533dfd-b363e65-100200-16de352a97f161; miid=242858321442603869; uc3=nk2=F5Qqa8SV13URpg%3D%3D&vt3=F8dByuQEZVeR5M8gZCs%3D&lg2=VT5L2FSpMGV7TQ%3D%3D&id2=VAMR5tzde4Qy; lgc=tb_3807958; uc4=nk4=0%40FY5hVasRUV%2BAMkETSvatE55w%2Brz4&id4=0%40VhpM3VXV6Gzb%2BkGVX8YOZF4uf7s%3D; _cc_=WqG3DMC9EA%3D%3D; mt=ci=5_1; v=0; cookie2=1706d501a4836493484bee96d648b676; _tb_token_=e7636edff7a65; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; JSESSIONID=63F113CF64151B33F16F18787EB0CDD9; isg=BHV1IRtuRNIClqDxlnbKr3oxhPHvWiljVmzRN_easew7zpXAv0Px1IEHHdLdokG8; l=dBjTqFGuqwu3cZlKBOfCnurza77OoIRb4EVzaNbMiICPOfCp5izAWZKPZWT9CnhVH6mpR3Jt3efvBeYBq6BQn5U62j-labkqndC..'
}
def getHTMLText(url):
try:
r = requests.get(url,headers = headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return '失败1'
def parsePage(ilt,html):
try:
plt = re.findall(r'"vies_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("失败2")
def printGoodList(ilt):
tplt = "{:4}\t{:8}\t{:16}"
print(tplt.format("序号","价格","商品名称"))
count = 0
for g in ilt:
count = count + 1
print(tplt.format(count,g[0],g[1]))
print('')
def main():
goods = '书包'
depth = 2
start_url = 'https://s.taobao.com/search?q=' + goods
getHTMLText(start_url)
infoList = []
for i in range(depth):
try:
url = start_url + '&s=' + str(44*i)
html = getHTMLText(url)
parsePage(infoList,html)
except:
continue
printGoodList(infoList)
main()
代码如上,为什么输出的结果只有"序号","价格","商品名称"这三个单词?问题出在哪?怎么解决啊~~~~
稍微仔细一点,就可
|
|