爬虫:淘宝价格

 1 import requests
 2 import re
 3 
 4 def getHTMLText(url):
 5     try:
 6         r = requests.get(url, timeout = 30)
 7         r.raise_for_status()
 8         r.encoding = r.apparent_encoding
 9         return r.text
10     except:
11         return "error_1"
12 
13 def parsePage(ilt, html):
14     try:
15         plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
16         tlt = re.findall(r'\"raw_title\"\:\".*?\"', html)
17         for i in range(len(plt)):
18             price = eval(plt[i].split(':')[1])
19             title = eval(tlt[i].split(':')[1])
20             ilt.append([price, title])
21     except:
22         print("error_2")
23 
24 def printGoodsList(ilt):
25     tplt = "{:4}\t{:8}\t{:16}"
26     print(tplt.format("序号", "价格", "商品名称"))
27     count = 0
28     for g in ilt:
29         count = count + 1
30         print(tplt.format(count, g[0], g[1]))
31 
32 def main():
33     goods = '书包'
34     depth = 2
35     start_url = 'https://s.taobao.com/search?q=' + goods
36     infoList = []
37     for i in range(depth):
38         try:
39             url = start_url + '&s=' + str(44*i)
40             html = getHTMLText(url)
41             parsePage(infoList, html)
42         except:
43             continue
44     printGoodsList(infoList)
45 
46 main()

 

转载于:https://www.cnblogs.com/dalanjing/p/7063051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值