Python爬虫——实战二:爬取天猫产品价格(逆向工程方法)

天猫上的产品价格请求URL的分析过程和爬京东价格的时候是类似的。
通过分析,得到天猫商品价格的请求URL:’https://mdskip.taobao.com/core/initItemDetail.htm?itemId=556708482118(这个是简化之后的,可用)。但是这个网页打开之后出现403 Forbidden 错误。这是因为在发送请求的时候需要添加Referer参数,其格式为“https://detail.tmall.com/item.htm?id=556708482118”。

代码

#-*-coding:utf-8 -*-
import urllib2
import json

def tmall_price(url):
    id = url.split("=")[-1]
    headers = {"Referer": "https://detail.tmall.com/item.htm?id={}".format(id)}
    request = urllib2.Request(url, headers=headers)
    response = urllib2.urlopen(request)
    html = response.read().decode("gbk")
    result = json.loads(html)
    price_info = result["defaultModel"]["itemPriceResultDO"]["priceInfo"]
    print price_info[price_info.keys()[0]]["promotionList"][0]["price"]

if __name__=="__main__":
    url = 'https://mdskip.taobao.com/core/initItemDetail.htm?itemId=556708482118'
    tmall_price(url)

得到的数据有如下形式:

{u'isSuccess': True, 
u'defaultModel': 
    {u'deliveryDO': 
        {u'deliverySkuMap': 
            {u'default': 
                [{u'postage': u'\u5feb\u9012: 0.00 EMS: 0.00 ', u'postageFree': False, u'skuDeliveryAddress': u'\u5e7f\u4e1c\u5e7f\u5dde', u'arrivalNextDay': False, u'type': 0, u'arrivalThisDay': False}]
            }, 
        u'areaId': 330100, 
        u'destination': u'\u676d\u5dde\u5e02', 
        u'success': True, 
        u'deliveryAddress': u'\u5e7f\u4e1c\u5e7f\u5dde'}, 
    u'doubleEleven2014': 
        {u'showRightRecommendedArea': False, u'halfOffItem': False, u'showAtmosphere': False, u'success': True, u'step': 0, u'doubleElevenItem': False}, 
    u'itemPriceResultDO': 
        {u'areaId': 330100, 
        u'extraPromShowRealPrice': False, 
        u'success': True, 
        u'priceInfo': 
            {u'3446150153619': 
                {u'areaSold': True, 
                u'price': u'399.00', 
                u'promotionList': 
                    [{u'status': 2, 
                    u'postageFree': False, 
                    u'canBuyCouponNum': 0, 
                    u'extraPromType': 0, 
                    u'price': u'299.00', 
                    u'tfCartSupport': False, 
                    u'unLogBrandMember': False, 
                    u'promType': u'normal', 
                    u'amountRestriction': u'', 
                    u'amountPromLimit': 0, 
                    u'start': False, 
                    u'unLogTbvip': False, 
                    u'basePriceType': u'IcPrice', 
                    u'extraPromTextType': 0, 
                    u'startTime': 1497959518000L, 
                    u'tmallCartSupport': False, 
                    u'endTime': 1530028740000L, 
                    u'type': u'\u79cb\u88c5\u4e0a\u65b0', 
                    u'unLogShopVip': False, 
                    u'limitProm': False}], 
                u'onlyShowOnePrice': False, 
                u'sortOrder': 0}, 
            u'3446150153611': ...,              
            u'3446150153612': ...,          
            u'3446150153613': ..., 
            ...
        u'tmallShopProm': ...

这里关注的只有价格信息,其结构为defaultModel–>itemPriceResultDO–>priceInfo–>3446150153619–>promotionList–>0–>price。

在实际爬取多个产品价格时,还发现另外一种价格信息结构:
defaultModel–>itemPriceResultDO–>priceInfo–>3446150153619–>price。

但是为什么有这么多个价格,3446150153619之类的字符串表示什么??

你可能感兴趣的:(编程语言Python)