gpt4 book ai didi

python - 单击上下文菜单项后验证当前 URL

转载 作者:行者123 更新时间:2023-11-30 23:20:56 25 4
gpt4 key购买 nike

我正在编写一个 python 脚本,右键单击一个文件,然后单击上下文菜单项。单击它会打开一个网页。我必须验证网页的网址。如何将控制权从操作系统转移到浏览器并获取当前 URL。

最佳答案

为什么不调用 requests.get(url) 并检查响应代码。另一种选择是调用 request.head(url)

>>> import requests
>>> url1 = 'http://example.com'
>>> url2 = 'http://sdsdsdsdsdss.com'
>>> r = requests.head(url1)
>>> r.status_code
200
>>> r = requests.head(url2, timeout=5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 77, in head
return request('head', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 387, in send
raise Timeout(e)
requests.exceptions.Timeout: (<urllib3.connectionpool.HTTPConnectionPool object at 0x7f4e77635950>, 'Connection to sdsdsdsdsdss.com timed out. (connect timeout=5)')
>>>

您需要处理异常。有关请求模块的详细信息:http://docs.python-requests.org/en/latest/

如果你确实需要打开网络浏览器,你可以使用这个库:https://docs.python.org/2/library/webbrowser.html

关于python - 单击上下文菜单项后验证当前 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25180462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com