安装库: Windows + R 输入cmd 后 执行下面这两行
pip install requests
pip install re
import requests
import re
废话不多说直接上源码,代码复制后就可以直接运行
import requests
import re
def open_excel():
poi_name_list = ['丽江', '三亚', '黄山', '九寨沟', '桂林']
return poi_name_list
def url_api(poi_name):
x = 1
for i in poi_name:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'referer': 'https://lbs.amap.com/tools/picker',
'cookie': 'cna=YhA8G/FDvhwCAdzDSBgviAYn; _uab_collina=166210157203123391584088; xlly_s=1; x5sec='
'7b227761676272696467652d616c69626162612d616d61703b32223a22313939373331396631353862333'
'9376466613365323335616432316337326637434b3376324a3447454a476b7237657374734c6c6e414577'
'684c715857454144227d; tfstk=cnKGBBxBJF76fGmvOCss2JJS2xhRZNZVZn-BYvPu90l18etFiG4Uzhs-K'
'1cS_r1..; l=fBOVy2enTjUrp1A3BO5CPurza77T3IRb8sPzaNbMiIEGa66h1FaWyNCeUHWWRdtjgT5YgUxz_'
'ANlDdHv5r4LRxOdew2F5156Vw968eZl2R2d.; isg=BDQ0Z2I367FyOX-i1sbGOVdKBfKmDVj3rWLYtM6VWr9COdWD_B9Hh3-_uXHhwZBP'
}
url = 'https://lbs.amap.com/_AMapService/v3/place/text?s=rsv3&key=f7d40927ba4d64fb91ebe2bb9cda0995&offset=' \
'1&page=1&extensions=all&city=110000&language=zh_cn&callback=jsonp_11165_&platform=JS&logversion=2.0&' \
'appname=https://lbs.amap.com/tools/picker&csid=8BF7376A-A135-40FA-9874-3E5DDD35375E&sdkversion=1.4.20&keywords={i}'
resp = requests.get(url, headers=headers).text
re_all = re.findall('"location":"(.*?)",', resp, re.S)
for result in re_all:
print(f'{x}位置:{i}, 坐标:{result}')
x += 1
if __name__ == '__main__':
poi_name = open_excel()
url_api(poi_name)
定义一个地方名列表
循环请求地名,解析获取到得数据,从中拿到有用数据