Let's now build a simple Weather Reporter application. The weather data for any given location will be fetched from the network, suitably formatted, and presented to the user.
We will use a higher level module named urllib to fetch weather data from the web. The urllib module is part of Python's standard library and it provides an easy to use API for working with URLs. It has four submodules:
- urllib.request: For opening and reading URLs
- urllib.error: For handling exceptions raised by urllib.request
- urllib.parse: For parsing URLs
- urllib.robotparser: For parsing robots.txt files
With urllib.request, fetching the contents of a web page turns into three lines of code (see 9.05_urllib_demo.py):
import urllib.request
with urllib.request.urlopen('http://www.packtpub.com/') as f:
print(f.read())
This prints the entire HTML...