import csvimport requests
# 定义列表转字符串函数
def list_to_str(i):
if i == []:
# 如果为空数组,则将其设为 None 或者某个默认值
return None
else:
# 如果不为空,则继续使用原来的值
separator = ','
result = separator.join(i)
return result
def keyword_search(input_file=None, keywords=None, city=None, types=None, output_file='poi.csv'):
url = 'https://restapi.amap.com/v3/place/text'
pois = []
if input_file is not None:
# 读取包含地名的csv文件
with open(input_file, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
# 构造请求参数
page = 1
while True:
params = {
'key': 'YOUR KEY',
'keywords': row['名称'],
'city': row['城市'],
'citylimit':True,
'output': 'JSON',
'offset': 20, # 每页输出的POI数量,默认为20
'page': page,
'extensions':'all'
}
# 发送请求
response = requests.get(url, params=params)
data = response.json()#data是一个字典
print(response.text) # 打印出服务器的响应文本
if data["status"] == "1":
if not data["pois"]:#data["pois"]是一个列表
break
pois.extend(data['pois'])
else:
print(f"Error: {data['info']}")
break
page += 1
# 将结果保存到csv文件中
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','名称','类型','类型编码','地址', '经度','纬度',
'电话','邮编','网站','邮箱','省份编码','省份名称',
'城市编码','城市名称','区域编码','区域名称'])#writer.writerow只能写入传递格式为列表或者元组的参数
for poi in pois:
location = poi['location'].split(',')
writer.writerow([poi['id'],poi['name'],poi['type'],poi['typecode'],
poi['address'],location[0],location[1],poi['tel'],
list_to_str(poi['postcode']),list_to_str(poi['website']),list_to_str(poi['email']),
poi['pcode'],poi['pname'],
poi['citycode'],poi['cityname'],
poi['adcode'],poi['adname']])#poi['postcode'],poi['website'],poi['email']均是列表
elif city is not None or types is not None:
# 构造请求参数
page = 1
while True:
params = {
'key': 'YOUR KEY',
'city': city,
'types': types,
'citylimit':True,
'output': 'JSON',
'offset': '10', # 每页输出的POI数量,默认为20
'page': page,
'extensions':'all'
}
#发送请求
response = requests.get(url,params=params)
data = response.json()
if data["status"] == "1":
if not data["pois"]:
break
pois.extend(data["pois"])
else:
print(f"Error: {data['info']}")
break
page += 1
# 将结果保存到csv文件中
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','名称','类型','类型编码','地址', '经度','纬度',
'电话','邮编','网站','邮箱','省份编码','省份名称',
'城市编码','城市名称','区域编码','区域名称'])#writer.writerow只能写入传递格式为列表或者元组的参数
for poi in pois:
location = poi['location'].split(',')
writer.writerow([poi['id'],poi['name'],poi['type'],poi['typecode'],
poi['address'],location[0],location[1],poi['tel'],
list_to_str(poi['postcode']),list_to_str(poi['website']),list_to_str(poi['email']),
poi['pcode'],poi['pname'],
poi['citycode'],poi['cityname'],
poi['adcode'],poi['adname']])
#keyword_search(input_file='./测试.csv', output_file='地名_POI.csv')
keyword_search(city='310000',types='120300',output_file='上海_住宅区.csv')
(2)url直接传递参数。
[Python] 纯文本查看复制代码
import csv
import requests
# 定义列表转字符串函数
def list_to_str(i):
if i == []:
# 如果为空数组,则将其设为 None 或者某个默认值
return None
else:
# 如果不为空,则继续使用原来的值
separator = ','
result = separator.join(i)
return result
def keyword_search(input_file=None, keywords=None, city=None, types=None, output_file='poi.csv'):
key = 'YOUR KEY'
citylimit = 'true'
offset = 50
page = 1
extensions = 'all'
pois = []
if input_file is not None:
# 读取包含地名的csv文件
with open(input_file, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
while True:
keywords = row['名称']
city = row['城市']
# url中的请求参数为空而不是为None
if keywords is None:
keywords=''
if city is None:
city=''
if types is None:
types=''
# 发送请求
url = f'https://restapi.amap.com/v3/place/text?key={key}&keywords={keywords}&types={types}&city={city}&citylimit={citylimit}&offset={offset}&page={page}&extensions={extensions}'
response = requests.get(url)
data = response.json()#data是一个字典
print(response.text) # 打印出服务器的响应文本
if data["status"] == "1":
if not data["pois"]:#data["pois"]是一个列表
break
pois.extend(data['pois'])
else:
print(f"Error: {data['info']}")
break
page += 1
elif city is not None or types is not None:
while True:
# url中的请求参数为空而不是为None
if keywords is None:
keywords=''
if city is None:
city=''
if types is None:
types=''
# 发送请求
url = f'https://restapi.amap.com/v3/place/text?key={key}&keywords={keywords}&types={types}&city={city}&citylimit={citylimit}&offset={offset}&page={page}&extensions={extensions}'
response = requests.get(url)
data = response.json()#data是一个字典
print(response.text) # 打印出服务器的响应文本
if data["status"] == "1":
if not data["pois"]:#data["pois"]是一个列表
break
pois.extend(data['pois'])
else:
print(f"Error: {data['info']}")
break
page += 1
# 将结果保存到csv文件中
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','名称','类型','类型编码','地址', '经度','纬度',
'电话','邮编','网站','邮箱','省份编码','省份名称',
'城市编码','城市名称','区域编码','区域名称'])#writer.writerow只能写入传递格式为列表或者元组的参数
for poi in pois:
location = poi['location'].split(',')
writer.writerow([poi['id'],poi['name'],poi['type'],poi['typecode'],
poi['address'],location[0],location[1],poi['tel'],
list_to_str(poi['postcode']),list_to_str(poi['website']),list_to_str(poi['email']),
poi['pcode'],poi['pname'],
poi['citycode'],poi['cityname'],
poi['adcode'],poi['adname']])
#keyword_search(input_file='./测试.csv', output_file='地名_POI.csv')
keyword_search(city='310000',types='120300',output_file='上海市_住宅区.csv')
import csv
import requests
# 定义列表转字符串函数
def list_to_str(i):
if i == []:
# 如果为空数组,则将其设为 None 或者某个默认值
return None
else:
# 如果不为空,则继续使用原来的值
separator = ','
result = separator.join(i)
return result
def keyword_search(input_file=None, keywords=None, region=None, types=None, output_file='poi.csv'):
key = 'YOUR KEY'
city_limit = 'true'
page_size = 25
page_num = 1
pois = []
if input_file is not None:
# 读取包含地名的csv文件
with open(input_file, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
while True:
keywords = row['名称']
region = row['城市']
# url中的请求参数为空而不是为None
if keywords is None:
keywords=''
if region is None:
region=''
if types is None:
types=''
# 发送请求
url = f'https://restapi.amap.com/v5/place/text?key={key}&keywords={keywords}&types={types}®ion={region}&city_limit={city_limit}&page_size={page_size}&page_num={page_num}'
response = requests.get(url)
data = response.json()#data是一个字典
print(response.text) # 打印出服务器的响应文本
if data["status"] == "1":
if not data["pois"]:#data["pois"]是一个列表
break
pois.extend(data['pois'])
else:
print(f"Error: {data['info']}")
break
page_num += 1
elif region is not None or types is not None:
while True:
# url中的请求参数为空而不是为None
if keywords is None:
keywords=''
if region is None:
region=''
if types is None:
types=''
# 发送请求
url = f'https://restapi.amap.com/v5/place/text?key={key}&keywords={keywords}&types={types}®ion={region}&city_limit={city_limit}&page_size={page_size}&page_num={page_num}'
response = requests.get(url)
data = response.json()#data是一个字典
print(response.text) # 打印出服务器的响应文本
if data["status"] == "1":
if not data["pois"]:#data["pois"]是一个列表
break
pois.extend(data['pois'])
else:
print(f"Error: {data['info']}")
break
page_num += 1
# 将结果保存到csv文件中
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','名称','类型','类型编码','地址', '经度','纬度','省份编码','省份名称',
'城市编码','城市名称','区域编码','区域名称'])#writer.writerow只能写入传递格式为列表或者元组的参数
for poi in pois:
location = poi['location'].split(',')
writer.writerow([poi['id'],poi['name'],poi['type'],poi['typecode'],
poi['address'],location[0],location[1],
poi['pcode'],poi['pname'],
poi['citycode'],poi['cityname'],
poi['adcode'],poi['adname']])
#keyword_search(input_file='./测试.csv', output_file='地名_POI.csv')
keyword_search(region='310000',types='120302',output_file='上海市_住宅小区v5.csv')