Python批量獲取京東商品列表信息


今天在逛網站的時候無意間發現一個京東獲取單個商品價格接口:

http://p.3.cn/prices/mgets?skuIds=J_商品ID&type=1 用例

ps:商品ID這么獲取:http://item.jd.com/954086.html

於是我就從Google上找了利用此接口批量獲取價格信息的源碼,稍作修改使其支持Python3(PS:通過這段代碼,我對Python的易用性不再懷疑了,23333)

#!/usr/bin/env python
# -*- coding: utf-8 -*- 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

效果:

安利一下個人博客:https://cxfer.cn/2016/75.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM