The urllib module is a built-in Python library that allows you to access data on the internet and parse URLs easily. It's a very handy module that every Python programmer should know.
Installing urllib
Since
To import urllib, simply add this import statement at the top of your Python script:
import urllib.request
Or to import portions of the urllib module:
from urllib import parse
And that's it! The urllib module is now ready for you to use in your code.
Using urllib
Here is a simple example to fetch a web page using urllib:
import urllib.request
with urllib.request.urlopen('http://www.python.org') as response:
html = response.read()
print(html)
This opens the Python website, reads the HTML content, and prints it out.
The
And more. Each submodule of urllib has several utility functions for handling URLs, querying web services, parsing responses, etc.
Next Steps
With urllib installed, you can now:
The possibilities are endless. Urllib makes many web-based tasks easy in Python. Check out the official documentation for more details and code examples.