zoukankan      html  css  js  c++  java
  • python 获取gearbest地址库代码

    import requests
    import json
    
    # 用来去掉多余的字符,并格式化
    def geshihua(str):
        s = None
        if "/**/_get_country(" in str:
            m = str.index('/**/_get_country(')+17
            s = str[m:-2]
        elif '/**/_user_get_province' in str:
            m = str.index('/**/_user_get_province') + 23
            s = str[m:-2]
        elif '/**/_user_get_city' in str:
            m = str.index('/**/_user_get_city') + 19
            s = str[m:-2]
        s = json.loads(s)
        s = s['data']
        return s
    
    def country():
        requests.packages.urllib3.disable_warnings()
        user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
        headers = {'User-Agent': user_agent}
        url = "https://www.gearbest.com/get-country?callback=_get_country"
        response = requests.get(url,headers)
        countrys = response.content.decode()
        countrys = geshihua(countrys)
        return countrys
    
    def province(countryCode):
        requests.packages.urllib3.disable_warnings()
        user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
        headers = {'User-Agent': user_agent}
        url = "https://www.gearbest.com/user/get-province?callback=_user_get_province&countryCode=" + countryCode
        response = requests.get(url, headers)
        provinces = response.content.decode()
        provinces = geshihua(provinces)
        return provinces
    
    def city(provinceCode):
        requests.packages.urllib3.disable_warnings()
        user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
        headers = {'User-Agent': user_agent}
        url = "https://user.gearbest.com/user/get-city?callback=_user_get_city&provinceCode=" + str(provinceCode)
        response = requests.get(url, headers)
        citys = response.content.decode()
        citys = geshihua(citys)
        return citys
    
    
    def main():
        f = open('address.txt', 'a')
    
        countrys = country()
        for c in countrys:
            countryName = c['countryName']
            # print(countryName)
            provinces = province(c['countryCode'])
            for p in provinces:
                provinceName = p['provinceName']
                # print(p['provinceCode'])
                citys = city(p['cdpId'])
                # print(citys)
                if len(citys) != 0 :
                    for t in citys:
                        cityName = t['cityName']
                        f.write(countryName + ',' + provinceName + ',' + cityName + '
    ')
                        print(countryName + ',' + provinceName + ',' + cityName)
                else:
                    f.write(countryName + ',' + provinceName + ',None' + '
    ')
                    print(countryName + ',' + provinceName + ',None')
        f.close()
    
        
    if __name__=="__main__":
        main()
  • 相关阅读:
    PHP 获取某年第几周的开始日期和结束日期的实例
    PHP科学计数法转换成数字
    laravel 辅助函数
    laravel5.3之后可以使用withCount()这个方法
    laravel 5.1 Model 属性详解
    laravel的启动过程解析
    转:按需加载html 图片 css js
    移动平台WEB前端开发技巧汇总(转)
    php重定向页面的三种方式
    zepto API参考(~~比较全面)
  • 原文地址:https://www.cnblogs.com/yyxianren/p/10694580.html
Copyright © 2011-2022 走看看