在python或类似的地方"静默地"打开URL

Ari*_*Ari 4 python batch-file

无论如何在Python中打开URL而不在浏览器中打开选项卡?

我有一个预构建的URL,我想在计算机空闲时运行,例如http://foo.com/bar

我已经能够使用startwebbrowser方法,但这些在我的浏览器中打开一个新选项卡.

仅供参考该URL返回一个空白页面,其中只有OK我不需要的单词.

任何人都可以建议任何其他方式,如通过批处理或任何其他程序可以做到这一点?

fal*_*tru 7

使用urllib(Python 2.x):

import urllib
urllib.urlopen('http://foo.com/bar')
Run Code Online (Sandbox Code Playgroud)

(Python 3.x)

import urllib.request
urllib.request.urlopen('http://foo.com/bar')
Run Code Online (Sandbox Code Playgroud)


Sam*_*lls 5

您还可以查看请求模块.如果您不需要实际内容,可以查看HTTP状态代码?

import requests
result = requests.get('http://foo.com/bar')

if(result.status_code == 200): print("OK")
Run Code Online (Sandbox Code Playgroud)