|
|

楼主 |
发表于 2019-3-23 15:20:31
|
显示全部楼层
def getHTML(url):
try:
r = requests.get(url,timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_enconding
return r.text
except:
return ''
def paresHTML(ilt,html):
try:
plt = re.findall(r'"view_price":"[\d\.]*"',html) #正则:搜索所有'view_price'字符串开头并包含字符串后面的数字信息(价格),返回列表
tlt = re.findall(r'"raw_tltle":".*?"',html)
for i in range(len(plt)):
price = eval(plt[i].split(':')[1]) #eval去掉引号
title = eval(tlt[i].spilt(':')[1])
ilt.append([prince,title])
except:
print('')
def printGoods(ilt):
tplt = '{:^6}\t{:^10}\t{:^16}'
print(tplt.format('序号','价格','名字'))
count = 0
for g in ilt:
count += 1
print(tplt.format(count,u[0],u[1]))
def main():
goods = '神舟笔记本'
start_url = 'https://s.taobao.com/search?q=' + goods
depth = 2
infoList = []
for i in range(depth):
try:
url = start_url + '&s=' + str(44*i)
html = getHTML(url)
parseHTML(infoList,html)
except:
continue
printGoods(infoList)
main()
|
|