首页 > 解决方案 > Python 3 requests.get() 在我公司的互联网上不起作用。解决方法?

问题描述

当我在我的个人 wifi 上的 python3 上使用 requests.get 时,它工作正常。然而,我试图自动化的事情发生在工作中。这是我的代码

import requests

res = requests.get('https://automatetheboringstuff.com/files/rj.txt')

工作时出现以下错误

Traceback (most recent call last):
  File "C:\Users\\[redacted]\AppData\Roaming\Python\Python37\site-packages\urllib3\connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "C:\Users\\[redacted]\AppData\Roaming\Python\Python37\site-packages\urllib3\util\connection.py", line 80, in create_connection
    raise err
  File "C:\Users\\[redacted]\AppData\Roaming\Python\Python37\site-packages\urllib3\util\connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

看起来防火墙阻止我下载任何文件。有没有办法解决?

编辑:我可以从浏览器手动下载这个文件。

标签: pythonpython-requestsfirewall

解决方案


这可能是由于代理。如果您正在使用代理,请检查您的 PC,如果您记下 ip 并更改您的代码,如下所示:

import requests

proxies = {
    "http": "http://<proxy-ip>",
    "https": "https://<proxy-ip>",
}
res = requests.get('https://automatetheboringstuff.com/files/rj.txt', proxies=proxies)

推荐阅读