Open a webpage using the urllib library.
- Details
- Category: 12th class computer practical's.
- Published: Friday, 11 June 2021 08:01
- Written by Super User
- Hits: 302
import urllib.request as url
import webbrowser as browser
code=url.urlopen('https://google.com')
print('code :',code.getcode())
if(code.getcode()==200):
page=code.read()
webbrowser.open('https://google.com', new=2)
else:
print("Some error with page request")
# 200 means request completed successfully
Explanation: In this program we are trying to open a web URL(uniform resource locator). to locate a website, network or a device on internet we have to use URL. typically URL on internet uses http protocol in networks.
url library is imported to check and open a url. webbrowser module is imported to open a URL in default web browser of your system. A web browser is a Application, which we use to open network locations and Websites over internet. code variable is storing code returned by requested url in urlopen function.
if Code returned by function is 200, it means request completed successfully and data is retuned to your system. open function of webbrowser module opens the data returned by URL and display it using default web browser of your system. otherwise error message is displayed.