Using Python to access network data Python network-data sixth (2)

Source: Internet
Author: User
Tags urlencode

Welcome Wu Mingying from Using Python to Access Web Data

Xyour answer is correct, score saved.

Your current grade in this assignment is:100%

Calling a JSON API

In this assignment you'll write a Python program somewhat similar to http://www.pythonlearn.com/code/geojson.py. The program would prompt for a location, contact a Web service and retrieve JSON for the Web service and parse that data, a nd retrieve the first place_id from the JSON. A place ID was a textual identifier that uniquely identifies a place as within Google Maps.

API End Points

To the "This assignment", you should use the "this" API endpoint that have a static subset of the Google Data:

Http://python-data.dr-chuck.net/geojson

This API uses the same parameters (sensor and address) as the Google API. This API also have no rate limit so you can test as often as. If you visit the URL with no parameters, you get a list of all of the address values which can is used with this API.

To call the API, you need to provide a sensor=false parameter and the address that is requesting as the address= parameter that's properly URL encoded using the Urllib.urlencode () fuction as shown in http://www. pythonlearn.com/code/geojson.py

Test data/sample Execution

You can test to see if your program was working with a location of ' South Federal University ' which would have a place_i D of "Chijj8oo7_b_bicr2alhc8nklok".

Turn in

Please run your. place_id to find the

Belorussian State University

Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint:the first seven characters of the place_id is "chijs3v ..."

Make sure to retreive the data from the URL specified above and not the normal Google API. Your program should work with the Google api-but, the place_id may not match for this assignment.


Python Code:

1 ImportUrllib2 ImportJSON3 4serviceurl ='Http://python-data.dr-chuck.net/geojson?'5 6  whileTrue:7Address = Raw_input ('Enter Location:')8url = serviceurl + urllib.urlencode ({'sensor':'false','Address': Address})9     Print 'Retrieving', the URLTenUH =urllib.urlopen (URL) Onedata =Uh.read () A     Print 'Retrieved', Len (data),'characters' -input =urllib.urlopen (URL). Read () -JS =json.loads (Input) the     Print 'Place ID', js['Results'][0]['place_id']

Using Python to access network data Python network-data sixth (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.