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. python datetime object 去除毫秒(microsecond)

    >>>import datetime >>>d = datetime.datetime.now().replace(microsecond=0) >>& ...

  2. [20181109]12c sqlplus rowprefetch参数5

    [20181109]12c sqlplus rowprefetch参数5.txt --//这几天一直在探究设置sqlplus参数rowprefetch与arraysize的关系,有必要做一些总结以及一 ...

  3. 洗礼灵魂,修炼python(68)--爬虫篇—番外篇之webbrowser模块

    题外话: 爬虫学到这里,我想你大部分的网站已经不再话下了对吧?有检测报文头的,我们可以伪造报文头为浏览器,有检测IP,我们可以用代理IP,有检测请求速度的,我们可以用time模块停顿一下,需要登录验证 ...

  4. sql server 分组,取每组的前几行数据

    sql中group by后,获取每组中的前N行数据,目前我知道的有2种方法 比如有个成绩表: 里面有字段学生ID,科目,成绩.我现在想取每个科目的头三名. 1.   子查询 select * from ...

  5. shell编程 学好内功(一)

    shell 背景 什么是shell编程 高大上的解释,往往让人摸不住头脑.一句话概括就是:shell编程就是对一堆Linux命令的逻辑化处理. 为什么要会shell编程 举个简单的例子,我们做java ...

  6. Golang 并发简介

    并发概要 随着多核CPU的普及, 为了更快的处理任务, 出现了各种并发编程的模型, 主要有以下几种: 模型名称 优点 缺点 多进程 简单, 隔离性好, 进程间几乎无影响 开销最大 多线程 目前使用最多 ...

  7. [福大软工] Z班 第1次成绩排行榜

    作业地址 http://www.cnblogs.com/easteast/p/7403491.html 作业要求 (1)回想一下你初入大学时对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的? ...

  8. 推酷文章中编写js组件系列文章整理

    一步一步实现JS拖拽插件 http://www.tuicool.com/articles/RBbmMjY JS组件系列——基于Bootstrap Ace模板的菜单和Tab页效果分享 http://ww ...

  9. MySQL高级知识(九)——慢查询日志

    前言:慢查询日志是MySQL提供的一种日志记录,它记录MySQL中响应时间超过阈值的语句,具体指运行时间超过long_query_time值的sql语句,该sql语句会被记录到慢查询日志中.慢查询日志 ...

  10. java中伪共享问题

    伪共享(False Sharing) 原文地址:http://ifeve.com/false-sharing/ 作者:Martin Thompson  译者:丁一 缓存系统中是以缓存行(cache l ...