[英]Python: How to go to a link and open it if it exists
我找到了這段代碼:
import webbrowser
webbrowser.open('http://eample.com')
如何修改它,只有在返回代碼為200時才會打開鏈接?
使用requests
包將HEAD
請求發送到所需位置,然后創建if
if requests.head(url).status_code == 200: webbrowser.open(url) # open url if status is 200
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.