Python‎ > ‎webbrowser‎ > ‎

webbrowser.open_new()


Definition
The method open the request in new default browser.

Syntax
webbrowser.open_new()

Parameters
string

Examples
The headers returns with the server response to get request and if statement is used to open the webbrowser condition to dict.keys.
 >>>import requests
>>> re=requests.get('http://www.pythonlake.com', params='Pythonlake')
>>> re.headers
{'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}
>>> 
>>> h.keys
<bound method CaseInsensitiveDict.keys of {'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}>
>>> if 'server' in h.keys():
...     >>> webbrowser.open_new('www.pythonlake.com')
True
>>>


If statement is used on the method of requests.request() to get the open link in the webbrowser.
 >>> import requests
>>> re=requests.request('Get','http://www.pythonlake.com')
>>> re
<Response [200]>
>>> if re.ok==True:
...     webbrowser.open_new('www.adilwaseem.com')
...     
True
>>> 


The returns are:
 import webbrowser

x=input("Please enter your web address: ")
y=str(x)

if y.startswith("y"):
    print("Access denied")
    webbrowser.open_new(y)

>>> 
Please enter your web address: yellow.com
Access denied

Comments