如何在不使用系统配置代理的情况下使用requests库

1 投票
1 回答
559 浏览
提问于 2025-04-18 17:15

如果我给 proxies 参数传入 None 或一个空的 dict,那么 requests 会自动使用操作系统中配置的代理,这些代理是通过 urllib.request.getproxies()(Python 3)或 urllib.getproxies() 获取的。

import requests
r = requests.get('http://google.com', proxies = {}) # or = None...
print(r.text)

如果我指定 proxies = { 'http': False },出于某种奇怪的原因,requests 甚至会完全卡住。

那么,我该如何让 requests 直接进行 HTTP 请求,而不使用任何代理呢?

1 个回答

3

结果发现,如果你想直接连接,就得把协议设置为空字符串:

r = requests.get('http://google.com', proxies = { 'http': '', ... })

这听起来有点奇怪,但生活就是这样。

撰写回答