Mark Read
Mark Read

Reputation: 137

Python: How to go to a link and open it if it exists

I found this code:

import webbrowser

webbrowser.open('http://eample.com')

How do I modify it so that it will only open the link if the return code is 200?

Upvotes: 1

Views: 583

Answers (1)

Malik Brahimi
Malik Brahimi

Reputation: 16711

Use the requests package to send a HEAD request to the desired location and then create an if

if requests.head(url).status_code == 200: webbrowser.open(url) # open url if status is 200

Upvotes: 3

Related Questions