今天在逛网站的时候无意间发现一个京东获取单个商品价格接口:
http://p.3.cn/prices/mgets?skuIds=J_商品ID&type=1
ps:商品ID这么获取:http://item.jd.com/954086.html
于是我就从Google上找了利用此接口批量获取价格信息的源码,稍作修改使其支持Python3(PS:通过这段代码,我对Python的易用性不再怀疑了,23333)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #!/usr/bin/env python
import requests from bs4 import BeautifulSoup
url = 'http://list.jd.com/list.html?cat=9987,653,655&page=1&delivery=1&trans=1&JL=4_21_0' request = requests.get(url) soup = BeautifulSoup(request.text, "html.parser") items = soup.select('li.gl-item') i = 1 for item in items: sku = item.find('div')['data-sku'] price_url = 'http://p.3.cn/prices/mgets?skuIds=J_' + str(sku) price = requests.get(price_url).json()[0]['p'] name = item.find('div', class_="p-name").find('em').string item_url = 'http:' + item.find('div', class_="p-name").find('a')['href'] commit = item.find('div', class_="p-commit").find('a').string print("%d、\n 名称: %s \n 价格: %s 元 \n 评价: %s 个 \n 链接: %s" % (i, name, price, commit, item_url)) if i >= 10: break else: i += 1
|