爬虫实战-百度地图API的调用访问地图数据

import requests,json,time
#https://blog.csdn.net/suwenkun1126/article/details/78343150

def getjson(palace,page_num):
	headers = {
		'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
	}
	url='http://api.map.baidu.com/place/v2/search'
	params={
		'q':'公园',
		'region':palace,
		'scope':'2',
		'page_size':'20',
		'page_num':page_num,
		'output':'json',
		'ak':'个人到百度地图api申请',
	}
	response=requests.get(url=url,params=params,headers=headers)
	html=response.text
	decodejson=json.loads(html)
	return decodejson

def get_place(place):
	place_list=place['results']
	time.sleep(1)
	for place in place_list:
		item={}
		item['name']=place['name']
		item['address']=place['address']
		item['uid']=place['uid']

		print(item)


def get_province(place):
	for eachcity in place['results']:
		city = eachcity['name']
		num = eachcity['num']
		output = '\t'.join([city, str(num)]) + '\r\n'
		print(output)
		with open('cities.txt', "a+", encoding='utf-8') as f:
			f.write(output)


def get_six_city(place):
	pass




#获取国家的uid
# province_list = ['江苏省', '浙江省', '广东省', '福建省', '山东省', '河南省', '河北省', '四川省', '辽宁省', '云南省',
# 	                 '湖南省', '湖北省', '江西省', '安徽省', '山西省', '广西壮族自治区', '陕西省', '黑龙江省', '内蒙古自治区',
# 	                 '贵州省', '吉林省', '甘肃省', '新疆维吾尔自治区', '海南省', '宁夏回族自治区', '青海省', '西藏自治区']
#
#
# for eachprovince in province_list:
# 	place=getjson(eachprovince)
# 	#print(place)
# 	get_province(place)
# 	#get_six_city(place)
#
city_list=[]
with open('cities.txt', "r", encoding='utf-8') as f:
	for eachline in f:
		if eachline != '' and eachline != '\n':
			city = eachline.split('\t')[0]
			city_list.append(city)
	f.close()
#print(city_list)

for city in city_list:
	not_last_page = True
	page_num = 0
	while not_last_page:
		place = getjson(city,page_num)
		print(city, page_num)
		if place['results']:
			get_place(place)
			page_num+=1
		else:
			not_last_page=False


#第二个API接口可以获得城市公园 的详细信息
#2、http://api.map.baidu.com/place/v2/detail?uid=xxxxx&output=json&scope=2&ak=用户的访问密匙

 

你可能感兴趣的:(爬虫)