簡體   English   中英

Python:如果存在,如何轉到鏈接並打開它

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM