马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# -*-coding:UTF-8-*-
'''根据行政区域查询'''
import json
import csv
import sys
import requests # 导入requests库,这是一个第三方库,把网页上的内容爬下来用的
ty = sys.getfilesystemencoding()
# print(ty)#这个可以获取文件系统的编码形式
import time
las = 1 # 给las一个值1
ak = 'Eugu5mRqawvsoNRwl9VOmzl0bOOB0eip'
# push=r'D:\python'
out = open('school.csv', 'a', newline='')
csv_write = csv.writer(out, dialect='excel')
print(time.time())
print('开始')
urls = [] # 声明一个数组列表
que = '美食'
ta = '餐厅,火锅,食堂'
for i in range(0, 20):
page_num = str(i)
url = 'http://api.map.baidu.com/place/v2/search?query=' + que + '&' \
'tag=' + ta + '®ion=宁波&page_size=20&page_num=' + str(
page_num) + '&output=json&ak=' + ak
urls.append(url)
# urls.append(url)的意思是,将url添加入urls这个列表中。
# f=open(r'D:\python\guiyangxuexiao.csv','a',encoding='utf-8'
print('url列表读取完成')
for url in urls:
time.sleep(5) # 为了防止并发量报警,设置了一个5秒的休眠。
print(url)
html = requests.get(url) # 获取网页信息
data = html.json() # 获取网页信息的json格式数据
print(data)
for item in data['results']:
jname1 = item['province']
jname2 = item['city']
jname3 = item['area']
jname4 = item['name']
jname = jname1 + jname2 + jname3 + jname4
j_uid = item['uid']
jstreet_id = item.get('street_id')
jlat = item['location']['lat']
jlon = item['location']['lng']
jaddress = item['address']
jphone = item.get('telephone')
j_str = (jname, j_uid, jstreet_id, str(jlat), str(jlon), jaddress, jphone)
print(j_str)
csv_write.writerow(j_str)
print("write over")
# f.write(j_str)
print(time.time())
# f.close()
print('完成')
|