代码拉取完成,页面将自动刷新
# 根据行政区划查找阿里地图的边界
import requests
import json
import time
from lxml import html
etree = html.etree
# 打开一个json文件
data = open("./area.json", encoding='utf-8')
# 转换为python对象
json_result = json.load(data)
# print(json_result)
for area in json_result:
areacode = area['areacode']
areaname = area['areaname']
areacode_2 = areacode[: 2]
if int(areacode_2) > 0:
if areacode[-4:] == '0000':
print('\033[31m province---------->', areacode)
time.sleep(1)
try:
if areacode == '710000':
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
else:
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '_full.json'
response = requests.get(full_url)
result = json.loads(response.text)
jsonfile = open('data/province/' + areacode + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile, indent=2, ensure_ascii=False)
jsonfile.close()
jsonfile1 = open('data/province1/' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile1, indent=2, ensure_ascii=False)
jsonfile1.close()
jsonfile2 = open('data/province2/' + areacode + '_' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile2, indent=2, ensure_ascii=False)
jsonfile2.close()
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
response = requests.get(full_url)
result = json.loads(response.text)
jsonfile_json = open('data/province_json/province/' + areacode + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile_json, indent=2, ensure_ascii=False)
jsonfile_json.close()
jsonfile_json1 = open('data/province_json/province1/' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile_json1, indent=2, ensure_ascii=False)
jsonfile_json1.close()
jsonfile_json2 = open('data/province_json/province2/' + areacode + '_' + areaname + '.json', 'w',
encoding='utf-8')
json.dump(result, jsonfile_json2, indent=2, ensure_ascii=False)
jsonfile_json2.close()
except:
print('\033[41m province错误')
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
jsonfile_error = open('data/province_error/' + areacode + '.json', 'w', encoding='utf-8')
result = {'areacode': areacode, 'url': full_url}
json.dump(result, jsonfile_error, indent=2, ensure_ascii=False)
jsonfile_error.close()
elif areacode[-2:] == '00':
print('\033[34m city---------->', areacode)
time.sleep(1)
error_list = ['441900', '442000', '460400', '620200']
if areacode in error_list:
pass
else:
try:
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '_full.json'
response = requests.get(full_url)
result = json.loads(response.text)
jsonfile = open('data/city/' + areacode + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile, indent=2, ensure_ascii=False)
jsonfile.close()
jsonfile1 = open('data/city1/' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile1, indent=2, ensure_ascii=False)
jsonfile1.close()
jsonfile2 = open('data/city2/' + areacode + '_' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile2, indent=2, ensure_ascii=False)
jsonfile2.close()
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
response = requests.get(full_url)
result = json.loads(response.text)
jsonfile_json = open('data/city_json/city/' + areacode + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile_json, indent=2, ensure_ascii=False)
jsonfile_json.close()
jsonfile_json1 = open('data/city_json/city1/' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile_json1, indent=2, ensure_ascii=False)
jsonfile_json1.close()
jsonfile_json2 = open('data/city_json/city2/' + areacode + '_' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile_json2, indent=2, ensure_ascii=False)
jsonfile_json2.close()
except:
print('\033[41m city错误')
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
jsonfile_error = open('data/city_error/' + areacode + '.json', 'w', encoding='utf-8')
result = {'areacode': areacode, 'url': full_url}
json.dump(result, jsonfile_error, indent=2, ensure_ascii=False)
jsonfile_error.close()
else:
print('\033[35m county---------->', areacode)
time.sleep(1)
error_list = ['320611', '330103', '330104', '350402', '410322']
if areacode in error_list:
pass
else:
try:
full_url = 'https://geo.datav.aliyun.com/areas_v3/bound/' + areacode + '.json'
response = requests.get(full_url)
result = json.loads(response.text)
jsonfile = open('data/county/' + areacode + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile, indent=2, ensure_ascii=False)
jsonfile.close()
jsonfile1 = open('data/county1/' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile1, indent=2, ensure_ascii=False)
jsonfile1.close()
jsonfile2 = open('data/county2/' + areacode + '_' + areaname + '.json', 'w', encoding='utf-8')
json.dump(result, jsonfile2, indent=2, ensure_ascii=False)
jsonfile2.close()
except:
print('\033[41m county错误')
jsonfile_error = open('data/county_error/' + areacode + '.json', 'w', encoding='utf-8')
result = {'areacode': areacode, 'url': full_url}
json.dump(result, jsonfile_error, indent=2, ensure_ascii=False)
jsonfile_error.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。