Python
15 Best Web Scraping Tools for Data Extraction in 2021
{loadposition top-ads-automation-testing-tools} Web scraping tools are specially developed...
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 gtupapers 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
Before we run the code to connect to Internet data, we need to import statement for URL library module or "urllib".
You can also read the HTML file by using the "read function" in Python, and when you run the code, the HTML file will appear in the console.
Here is the complete code
Python 2 Example
#
# read the data from the URL and print it
#
import urllib2
def main():
# open a connection to a URL using urllib2
webUrl = urllib2.urlopen("https://www.youtube.com/user/gtupaperscom")
#get the result code and print it
print "result code: " + str(webUrl.getcode())
# read the data from the URL and print it
data = webUrl.read()
print data
if __name__ == "__main__":
main()Python 3 Example
#
# read the data from the URL and print it
#
import urllib.request
# open a connection to a URL using urllib
webUrl = urllib.request.urlopen('https://www.youtube.com/user/gtupaperscom')
#get the result code and print it
print ("result code: " + str(webUrl.getcode()))
# read the data from the URL and print it
data = webUrl.read()
print (data)
{loadposition top-ads-automation-testing-tools} Web scraping tools are specially developed...
Python is one of the most popular programming languages. Currently, each of the following six...
OOPs in Python OOPs in Python is a programming approach that focuses on using objects and classes...
Python Copy File Methods Python provides in-built functions for easily copying files using the...
Dictionary is one of the important data types available in Python. The data in a dictionary is...
What is PyQt? PyQt is a python binding of the open-source widget-toolkit Qt, which also functions as...