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 + '\n')
print(countryName + ',' + provinceName + ',' + cityName)
else:
f.write(countryName + ',' + provinceName + ',None' + '\n')
print(countryName + ',' + provinceName + ',None')
f.close() if __name__=="__main__":
main()

python 获取gearbest地址库代码的更多相关文章

  1. python 获取 mac 地址 的代码

    python 获取 mac 地址 的例子,有需要的朋友可以参考下. #!/bin/python import os import re def GetMac():     if os.name ==  ...

  2. python获取内存地址上存储的值

    在python中,可以通过id()这个方法来获取对象的内存地址. 但是反过来,怎么获取内存地址上存储的值? 先看一段代码: from ctypes import string_at from sys ...

  3. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  4. python 获取mac地址zz

    通过python获取当前mac地址的方法如下:(1)通用方法,借助uuid模块def get_mac_address(): import uuid      node = uuid.getnode() ...

  5. python获取ip地址

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import socket,fcntl,struct #crontab下shell命令无 ...

  6. Python 获取 网卡 MAC 地址

    /*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...

  7. ios开发 iphone中获取网卡地址和ip地址

    这是获取网卡的硬件地址的代码,如果无法编译通过,记得把下面的这几个头文件加上把. #include <sys/socket.h> // Per msqr#include <sys/s ...

  8. iOS 获取IP地址

    一.获取本机IP地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #import <ifadd ...

  9. python获取文件路径

    摘自:https://blog.csdn.net/Poo_Chai/article/details/89764001 import os root_path = os.path.abspath(os. ...

随机推荐

  1. 商家服务无法上架提示没有授权信息解决FAQ

    1.地址授权:https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=2018032002416255&redirect_uri= ...

  2. MySQL Innodb数据库误删ibdata1后MySQL数据库的恢复案例

      上周,以前公司的同事朋友找我帮忙,看看能否帮忙恢复一个MySQL 数据库,具体情况为:数据库版本为MySQL 5.6(具体版本不清楚),也不清楚具体的数据库引擎; 没有数据库备份,只剩下数据库下面 ...

  3. [20190213]学习bbed-恢复删除的数据.txt

    [20190213]学习bbed-恢复删除的数据.txt --//以前也做过类似测试,当时在用bbed做verify时错误都不处理,当时的想法就是能读出就ok了.--//而且当时也做成功,纯粹是依葫芦 ...

  4. [20180822]session_cached_cursors与子游标堆0.txt

    [20180822]session_cached_cursors与子游标堆0.txt --//前几天测试刷新共享池与父子游标的问题,--//链接: http://blog.itpub.net/2672 ...

  5. SQL server分离和附加数据库

    下文是参考网友的文章结合自身的经验来总结的 应用于将数据库更改到同一计算机或不同计算机的不同SQL server实例中,或者要移动数据库文件存储的位置时. 一.分离数据库 将数据库从SQL serve ...

  6. selenium RC 环境配置

    摘自http://www.cnblogs.com/sanzangTst/p/7452922.html 收藏学习 学习selenium python需要的工具: 1.浏览器 2.Python 3.Sel ...

  7. C#获取一个实体类的属性名称、属性值

    using System.Reflection; Type t = obj.GetType();//获得该类的Type foreach (PropertyInfo pi in t.GetPropert ...

  8. c/c++ 数组的智能指针 使用

    数组的智能指针 使用 数组的智能指针的限制: 1,unique_ptr的数组智能指针,没有*和->操作,但支持下标操作[] 2,shared_ptr的数组智能指针,有*和->操作,但不支持 ...

  9. js获取select选中的内容

    ### 获取select选中的内容 js获取select标签选中的值 var obj = document.getElementById("selectId");//获取selec ...

  10. May 24. 2018 Week 21st Thursday

    Man errs so long as he strives. 失误是进取的代价. It is not important that the man in the arena didn't win, ...