Python|Python 小项目2 使用JSON API并处理数据
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geojson.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
【Python|Python 小项目2 使用JSON API并处理数据】这里是存为文本,然后提取资料。
import urllib.request, urllib.parse, urllib.error
import jsonfh = open('urlgeo.txt', 'w')# Note that Google is increasingly requiring keys
# for this API, recheve the information from the website
serviceurl = 'http://py4e-data.dr-chuck.net/geojson?'while True:
address = input('Enter location: ')
#address = 'University of Buenos Aires'
if len(address) < 1: breakurl = serviceurl + urllib.parse.urlencode(
{'address': address})print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = https://www.it610.com/article/uh.read().decode()
print('Retrieved', len(data), 'characters')try:
js = json.loads(data)
except:
js = Noneif not js or 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
print(data)
continueraw_data = https://www.it610.com/article/json.dumps(js, indent=4)
fh.write(raw_data+'\n')
fh.close()# load the file information into data
fh = open('urlgeo-1.txt')
data = https://www.it610.com/article/fh.read()
fh.close()# extract the inforamtion from Json
raw_handle = json.loads(data)
# print(len(raw_handle)), two keys are status and results
handle_results = raw_handle['results']#print(type(handle_results),len(handle_results)),
#this is a list with only one valuehandle_address = handle_results[0]
place_id = handle_address['place_id']
print(place_id)
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- iOS中的Block