爬取淘宝商品价格的小问题

[复制链接]
查看11 | 回复0 | 2021-1-27 05:47:52 | 显示全部楼层 |阅读模式
importrequests
importre
defgetHTMLText(url):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
returnr.text
except:
return""
defparsePage(ilt,html):
try:
plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
tlt=re.findall(r'\"raw_title\"\:\".*?\"',html)
foriinrange(len(plt)):
price=eval(plt.split(':')[1])
title=eval(tlt.split(':')[1])
ilt.append([price,title])
except:
print("")
defprintGoodsList(ilt):
tplt="{:4}\t{:8}\t{:16}"
print(tplt.format("序号","价格","商品名称"))
count=0
forginilt:
count=count+1
print(tplt.format(count,g[0],g[1]))
defmain():
goods='电脑'
depth=1
start_url='https://s.taobao.com/search?q='+goods
infoList=[]
foriinrange(depth):
try:
url=start_url+'&s='+str(44*i)
html=getHTMLText(url)
parsePage(infoList,html)
except:
continue
printGoodsList(infoList)
main()
运行结果:


分 -->
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行