使用 Tor 的 Python 请求

标签 python python-requests tor socks

无法将 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/

相关文章:

python - 将 ip 地址绑定(bind)到 urllib2 对我不起作用

python - 计算 GCD - 如何检查列表中的每个元素

使用 TOR 获取代理的 Python selenium 适用于 firefox,但不适用于 chrome

python - 使用包含文档作为字节的对象导出 Pandas DataFrame 时出现问题

python - Marketo API 和 Python,发布请求失败

python - 无法修正抓取下一页链接的逻辑,使执行速度更快

python - 如何通过 API 创建带标题的 Imgur 相册?

c++ - 通过 SOCKS5 c++ 发送和接收

python - 将当前对象传递给 python apscheduler 方法

Python:输入信息后从网络上抓取数据