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()

最新文章

  1. .NET平台开源项目速览(9)软件序列号生成组件SoftwareProtector介绍与使用
  2. linux系统免密码登陆
  3. Eclipse为成员变量设置前缀
  4. React Native 重新建项目遇到的一些问题
  5. Windows Phone 7之XNA游戏:重力感应
  6. win7登入使用的是临时档案解决方法
  7. VS中制作安装文件
  8. 使用WebJar管理css、JavaScript文件
  9. iOS: plist实例
  10. python global 全局变量
  11. Altium Designer 生成 Mach3 G代码的程序
  12. Java语言编程注意事项
  13. django相关网站
  14. Activiti6-TaskService(学习笔记)重要
  15. Jar包的手动导入
  16. excel表格公式无效、不生效的解决方案及常见问题、常用函数
  17. css3-study-new
  18. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
  19. World Finals 2017爆OJ记
  20. Linux配置中文输入法(搜狗输入法)

热门文章

  1. Using IntelliJ IDEA as the Vim Editor
  2. 利用uWSGI和nginx进行服务器部署
  3. Java的基础知识三
  4. Linux 小知识翻译 - 「虚拟化技术 续」
  5. Python开发【第二篇】:基本数据类型
  6. 监听器的配置,绑定HttpSessionListener监听器的使用
  7. js格式化输入框内金额、银行卡号[转]
  8. Linux CenterOS安装mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz步骤
  9. [2] LabelImg图片标注 与 YOLOv3 网络训练 (待补充)
  10. 机器学习算法总结(十二)——流形学习(Manifold Learning)