python 获取lazada菲律宾站地址库

import urllib3
import requests
import ast
import time

# 因为lazada返回的数据是json类型,通过解码成字符串类型,为了方便数据操作,使用字符串截取的方法将字符串格式化为包含字典的list

def geshihua(s):
    m = s.index('[')
    n = s.index(']')
    s = s[m:n + 1]
    return ast.literal_eval(s)

    province_list = ast.literal_eval(province)

# 通过国家代码获取省的名称及ID,返回包含字典的list
def sheng(guo):
    requests.packages.urllib3.disable_warnings()
    http = urllib3.PoolManager()
    url = "https://member.lazada.com.ph//locationtree/api/getSubAddressList?countryCode=" + guo
    res = http.request('GET', url)
    province = res.data.decode()
    province_list = geshihua(province)
    sheng = []
    for p in province_list:
        sheng.append({'id':p['id'],'name':p['displayName']})
    return sheng


# 通过国家代码及省ID或市ID获取市或者县的信息,返货包含字典的list
def shixian(guo,sheng):
    requests.packages.urllib3.disable_warnings()
    http = urllib3.PoolManager()
    url = "https://member.lazada.com.ph//locationtree/api/getSubAddressList?countryCode=" + guo +"&addressId="+ sheng
    res = http.request('GET', url)
    province = res.data.decode()
    province_list = geshihua(province)
    shixian = []
    for p in province_list:
        shixian.append({'id':p['id'],'name':p['displayName']})
    return shixian


# 只需要提供国家代码,即可遍历lazada该站的所有地名及编码
def main():

    f = open('address.txt','a')

    g = "PH"
    s = sheng(g)
    for s1 in s:
        shi = shixian(g,s1['id'])
        for s2 in shi:
            xian = shixian(g,s2['id'])
            for x in xian:
                f.write(s1['name']+','+s2['name']+','+x['name'])
                f.write('\n')
        time.sleep(1)



if __name__=="__main__":
    main()

 

转载于:https://www.cnblogs.com/yyxianren/p/10694567.html

你可能感兴趣的:(python 获取lazada菲律宾站地址库)