用Python获取中国大陆,香港和美国苹果官网iphone价格(2018版)

去年版本的脚本在此:
用Python获取中国大陆,香港,日本和美国苹果官网iphone价格
今年版本改成使用Python3.7,主要是获取iphone XR, XS和XS max的价格,其中美版无锁XR价格还没出来,先不获取。获取的价格根据最新汇率换算成RMB。

import re
import json
import urllib.request

url_us_xs_64 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT952LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"
url_us_xs_256 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT982LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"
url_us_xs_512 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT9C2LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"
url_us_xs_max_64 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT5A2LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"
url_us_xs_max_256 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT5E2LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"
url_us_xs_max_512 = "https://www.apple.com/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&purchaseOption=fullPrice&product=MT5H2LL%2FA&cppart=UNLOCKED%2FUS&carrierPolicyType=UNLOCKED"

us_iphone_dic = {
    "us iphone xs 64G" : url_us_xs_64, "us iphone xs 256G" : url_us_xs_256, "us iphone xs 512G" : url_us_xs_512, "us iphone xs max 64G" : url_us_xs_max_64,
    "us iphone xs max 256G" : url_us_xs_max_256, "us iphone xs max 512G" : url_us_xs_max_512
}

url_cn_xr_64 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT132CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xr_128 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT1A2CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xr_256 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT1J2CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_64 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT9Q2CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_256 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT9U2CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_512 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT9X2CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_max_64 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT732CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_max_256 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT762CH%2FA&carrierPolicyType=UNLOCKED"
url_cn_xs_max_512 = "https://www.apple.com/cn/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT792CH%2FA&carrierPolicyType=UNLOCKED"

cn_iphone_idc = {
    "china iphone xr 64G" : url_cn_xr_64, "china iphone xr 128G" : url_cn_xr_128, "china iphone xr 256G" : url_cn_xr_256,
    "china iphone xs 64G" : url_cn_xs_64, "china iphone xs 256G" : url_cn_xs_256, "china iphone xs 512G" : url_cn_xs_512, 
    "china iphone xs max 64G" : url_cn_xs_max_64, "china iphone xs max 256G" : url_cn_xs_max_256, "china iphone xs max 512G" : url_cn_xs_max_512
}

url_hk_xr_64 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT132ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xr_128= "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT1A2ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xr_256 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xr&step=select&product=MT1J2ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_64 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT952ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_256 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT982ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_512 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT9C2ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_max_64 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT732ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_max_256 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT762ZA%2FA&carrierPolicyType=UNLOCKED"
url_hk_xs_max_512 = "https://www.apple.com/hk/shop/updateSummary?node=home/shop_iphone/family/iphone_xs&step=select&product=MT792ZA%2FA&carrierPolicyType=UNLOCKED"

hk_iphone_idc = {
    "hongkong iphone xr 64G" : url_hk_xr_64, "hongkong iphone xr 128G" : url_hk_xr_128, "hongkong iphone xr 256G" : url_hk_xr_256,
    "hongkong iphone xs 64G" : url_hk_xs_64, "hongkong iphone xs 256G" : url_hk_xs_256, "hongkong iphone xs 512G" : url_hk_xs_512, 
    "hongkong iphone xs max 64G" : url_hk_xs_max_64, "hongkong iphone xs max 256G" : url_hk_xs_max_256, "hongkong iphone xs max 512G" : url_hk_xs_max_512
}

def get_exchange_rate(exchange_code):
    if exchange_code == 'CNY':
        return 1.0

    url = "http://webforex.hermes.hexun.com/forex/quotelist?code=FOREX" + exchange_code + "CNY&column=Code,Price"
    req = urllib.request.Request(url)
    f = urllib.request.urlopen(req)
    html = f.read().decode("utf-8")

    s = re.findall("{.*}",str(html))[0]
    sjson = json.loads(s)

    USDCNY = sjson["Data"][0][0][1]/10000
    return USDCNY

def get_price(dic, exchange_code):
    keys = dic.keys()
    l = sorted(keys)
    for des in l:
        url = dic[des]
        req = urllib.request.Request(url)
        f = urllib.request.urlopen(req)
        html = f.read().decode("utf-8")
        parsed_json = json.loads(html)
        seo_price = parsed_json['body']['response']['summarySection']['summary']['seoPrice']
        float_price = float(seo_price)
        print(des + "\t" + ': ' + str(float_price * get_exchange_rate(exchange_code)))

if __name__ == '__main__':
    get_price(cn_iphone_idc, 'CNY')
    get_price(us_iphone_dic, 'USD')
    get_price(hk_iphone_idc, 'HKD')
    print('---END---')

获取的结果:

china iphone xr 128G : 6999.0
china iphone xr 256G : 7899.0
china iphone xr 64G : 6499.0
china iphone xs 256G : 10099.0
china iphone xs 512G : 11899.0
china iphone xs 64G : 8699.0
china iphone xs max 256G : 10999.0
china iphone xs max 512G : 12799.0
china iphone xs max 64G : 9599.0
us iphone xs 256G : 7888.6893
us iphone xs 512G : 9261.829300000001
us iphone xs 64G : 6858.8343
us iphone xs max 256G : 8575.2593
us iphone xs max 512G : 9948.399300000001
us iphone xs max 64G : 7545.4043
hongkong iphone xs max 64G : 8314.474699999999
hongkong iphone xr 128G : 6038.6947
hongkong iphone xr 256G : 6826.4646999999995
hongkong iphone xr 64G : 5688.5747
hongkong iphone xs 256G : 8664.5947
hongkong iphone xs 512G : 10152.6047
hongkong iphone xs 64G : 7526.704699999999
hongkong iphone xs max 256G : 9452.3647
hongkong iphone xs max 512G : 10940.3747
---END---

更多文章欢迎访问个人博客

你可能感兴趣的:(用Python获取中国大陆,香港和美国苹果官网iphone价格(2018版))