今天小编给大家分享一下如何用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 + '\n')
print(countryName + ',' + provinceName + ',' + cityName)
else:
f.write(countryName + ',' + provinceName + ',None' + '\n')
print(countryName + ',' + provinceName + ',None')
f.close()
if __name__=="__main__":
main()
以上就是“如何用python获取gearbest地址库”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.51cto.com/u_472169/5720254