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

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

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

ps:商品ID這麼獲取:http://item.jd.com/954086.htmlgit

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

#!/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

效果:
json

安利一下我的博客:https://cxfer.cn/2016/75.html網站

相關文章
相關標籤/搜索