无法将 Tor 与 Python 请求一起使用
import requests
proxies = {
'http': 'socks5://localhost:9050',
'https': 'socks5://localhost:9050'
}
url = 'http://httpbin.org/ip'
print(requests.get(url, proxies=proxies).text)
我尝试了多种解决方案,但没有一个对我有用。我正在尝试通过 Tor 使用 Python 发出简单的请求。提前致谢。
错误:
requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='canihazip.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('< urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x031B77F0>: Failed to esta blish a new connection: [Errno 10061] No connection could be made because the ta rget machine actively refused it',))
最佳答案
首先确保您pip3 install requests[socks]
,或者如果使用 zsh,pip3 install "requests[socks]"
然后这样做:
import requests
session = requests.session()
proxies = {
'http': 'socks5h://localhost:9050',
'https': 'socks5h://localhost:9050'
}
session.get(url, proxies=proxies)
注意 socks5h://
h
此外,您还必须在计算机(而非浏览器)上运行 tor。您可以使用 homebrew 安装 tor通过运行 brew install tor
。
您只需在终端中运行 tor
即可启动一个 tor 实例。
关于使用 Tor 的 Python 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55425580/