某宁 价格爬取 python

苏宁易购价格爬取 python

[参考](https://www.cnblogs.com/cekong/p/10916498.html)
如果商品地址为:http://product.suning.com/0070230548/10608983060.html

则价格地址:

http://pas.suning.com/nspcsale_0_000000010608983060_000000010608983060_0070230548_20_021_0210101_500353_1000267_9264_12113_Z001___R9006849_3.3_1___000278188__.html?callback=pcData&_=1558663936729

如果商品地址为:http://product.suning.com/0000000000/144016246.html

则价格地址:

http://pas.suning.com/nspcsale_0_000000000144016246_000000000144016246_0000000000_20_021_0210101_500353_1000267_9264_12113_Z001___R9006850_2.86_0___000278188__.html?callback=pcData&_=1558664442552


def get_detail_by_url(url):
    res = re.findall("\d+", url)
    if res:
        # print(res)
        id_url1 = res[0]
        id_url2 = res[1]

    if '/0000000000/' not in url:

        base_url = f'http://pas.suning.com/nspcsale_0_0000000{id_url2}_0000000{id_url2}_{id_url1}_20_021_0210101_500353_1000267_9264_12113_Z001___R9006850_2.86_0___000278188__.html?callback=pcData&_=1558663936729'

    else:
        base_url = f'http://pas.suning.com/nspcsale_0_0000000{id_url2}_0000000{id_url2}_{id_url1}_20_021_0210101_500353_1000267_9264_12113_Z001___R9006849_3.3_1___000278188__.html?callback=pcData&_=1558664442552'
    # print(base_url)
    ret = requests.get(base_url)
    # print(ret.text)
    new_ret = ret.text[7:-2]
    json_data = json.loads(new_ret)

    lis = json_data.get('data', {}).get("price", {}).get("saleInfo", [])

    if lis:
        price1 = lis[0].get('promotionPrice')
        if price1:
            return price1
        else:
            print(json_data)
            return "未知价格"



if __name__ == '__main__':
 
     ret = get_detail_by_url('https://product.suning.com/0000000000/12182110758.html')

posted @ 2021-05-11 11:11  wzqwer  阅读(132)  评论(0编辑  收藏  举报