Python Internet Access using Urllib.Request and urlopen()

What is urllib?

urllib is a Python module that can be used for opening URLs. It defines functions and classes to help in URL actions.

With Python you can also access and retrieve data from the internet like XML, HTML, JSON, etc. You can also use Python to work with this data directly. In this tutorial we are going to see how we can retrieve data from the web. For example, here we used a guru99 video URL, and we are going to access this video URL using Python as well as print HTML file of this URL.

In this tutorial we will learn

  • Import urllib
  • Define your main function
  • Declare the variable webUrl
  • Then call the urlopen function on the URL lib library
  • The URL we are opening is guru99 tutorial on youtube
  • Next, we going to print the result code
  • Result code is retrieved by calling the getcode function on the webUrl variable we have created
  • We going to convert that to a string, so that it can be concatenated with our string “result code”
  • This will be a regular HTTP code “200”, indicating http request is processed successfully