前端时间
跟着大神用百度爬取需要的poi数据,无耐被封,小弟菜鸟一个,暂时没弄懂代理IP池的构建方法,所以就只能转向高德,以下是用于获取全国指定城市的高德adcode编码
from bs4 import BeautifulSoup
import requests,time,random
with open('全国各省.txt','r') as f:
g=f.read().split(',')
ak='a32b3f70f49ba1f0435da3ce55c92016'
# url='https://restapi.amap.com/v3/config/district?key=a32b3f70f49ba1f0435da3ce55c92016&keywords=贵州&subdistrict=2&extensions=base&ouput=json'
# req=requests.get('https://restapi.amap.com/v3/config/district?key='+ak+'&keywords=贵州&subdistrict=2&extensions=base&ouput=json')
for pro in g:
time.sleep(random.randint(9,20))
req=requests.get('https://restapi.amap.com/v3/config/district?key='+ak+'&keywords='+pro+'&subdistrict=2&extensions=base&ouput=json')
data=req.json()
i=data['districts'][0]['districts']
dic={}
print(pro)
for ad in i:
dis=ad['districts']
for area in dis:
dic[area['name']]=area['adcode']
with open(pro+'县区代码.txt','a') as f:
f.write(str(dic))
# with open('贵州省县区代码.txt','r+') as fr:
# dic=eval(fr.read())
# print(dic.values())