python中的tor ip旋转 - stem.socketerror:[errno 61]连接拒绝
我正在使用Python和Beutifuloup撰写Web Craper。
不久,我的IP就被阻止了。我现在需要旋转我的IP,以便可以连接到网站并刮擦所需的数据。
我主要遵循教程和git repo文档:
我只是按线路遵循教程,而不是100%确定,如果我做正确的事。
我已经将TORRC文件设置为:
# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1, and Tor will ignore it
ClientOnionAuthDir /Users/user/Library/Application Support/TorBrowser-Data/Tor/onion-auth
DataDirectory /Users/user/Library/Application Support/TorBrowser-Data/Tor
GeoIPFile /Applications/Tor Browser.app/Contents/Resources/TorBrowser/Tor/geoip
GeoIPv6File /Applications/Tor Browser.app/Contents/Resources/TorBrowser/Tor/geoip6
ControlPort 9051
HashedControlPassword my_hashed_password
CookieAuthentication 1
通过运行 tor-Hash-password my_password
获得的 my_hashed_password
。
我继续在目录中创建 config
文件,其中 privoxy
安装了以下内容:
forward-socks5 / 127.0.0.1:9050 .
每当我更改这两个文件中的某些内容时,我都会运行一个简短的脚本来重新启动服务并致电Privoxy检查所有内容都可以:
brew services restart tor
brew services restart privoxy
privoxy
当我运行一个测试脚本时:
import time
from urllib.request import ProxyHandler, build_opener, install_opener, Request, urlopen
from stem import Signal
from stem.control import Controller
class TorHandler:
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11'}
def open_url(self, url):
# communicate with TOR via a local proxy (privoxy)
def _set_url_proxy():
proxy_support = ProxyHandler({'http': '127.0.0.1:8118'})
opener = build_opener(proxy_support)
install_opener(opener)
_set_url_proxy()
request = Request(url, None, self.headers)
return urlopen(request).read().decode('utf-8')
@staticmethod
def renew_connection():
__TOR_password__ = 'my_password'
__TOR_hashed_password__ = 'my_hashed_password'
with Controller.from_port(port=9051) as controller:
controller.authenticate(password=__TOR_password__)
controller.signal(Signal.NEWNYM)
controller.close()
if __name__ == '__main__':
wait_time = 2
number_of_ip_rotations = 3
tor_handler = TorHandler()
ip = tor_handler.open_url('http://icanhazip.com/')
print('My first IP: {}'.format(ip))
# Cycle through the specified number of IP addresses via TOR
for i in range(0, number_of_ip_rotations):
old_ip = ip
seconds = 0
tor_handler.renew_connection()
# Loop until the 'new' IP address is different than the 'old' IP address,
# It may take the TOR network some time to effect a different IP address
while ip == old_ip:
time.sleep(wait_time)
seconds += wait_time
print('{} seconds elapsed awaiting a different IP address.'.format(seconds))
ip = tor_handler.open_url('http://icanhazip.com/')
print('My new IP: {}'.format(ip))
注意:我尝试了 tor_password 和 tor_hashed_password 。
我将收到以下输出:
"/Users/code/venv/bin/python" "/Users/code/proxy_rotation.py"
My first IP: 185.220.101.16
Traceback (most recent call last):
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 535, in _make_socket
control_socket.connect((self.address, self.port))
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/code/proxy_rotation.py", line 48, in <module>
tor_handler.renew_connection()
File "/Users/code/proxy_rotation.py", line 29, in renew_connection
with Controller.from_port(port=9051) as controller:
File "/Users/code/venv/lib/python3.8/site-packages/stem/control.py", line 1033, in from_port
control_port = stem.socket.ControlPort(address, port)
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 503, in __init__
self.connect()
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 172, in connect
self._socket = self._make_socket()
File "/Users/code/venv/lib/python3.8/site-packages/stem/socket.py", line 538, in _make_socket
raise stem.SocketError(exc)
stem.SocketError: [Errno 61] Connection refused
Process finished with exit code 1
我将不胜感激:
- 解决此问题,并使用TOR旋转IP,
- 以更好的方式在本地主机上与Python一起旋转IPS的任何建议。 请将其放在码头容器中
当我有这个工作时,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我的Tor没有开始。
当我手动启动它作为应用程序时,我可以连接并请求网站。
不得不看我如何自动启动tor而不必从应用程序启动它
It looks like my Tor was not started.
When I manually start it as an application I can connect and request a website.
Ill have to look at how I can automatically start Tor without having to start it from my applications