[英]Tor IP Rotation in Python - stem.SocketError: [Errno 61] Connection refused
我正在使用 Python 和 Beutifulsoup 编写网络爬虫。
没过多久我的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
我继续在安装privoxy
的目录中创建一个config
文件,其中包含以下内容:
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 没有启动。
当我手动将其作为应用程序启动时,我可以连接并请求一个网站。
我得看看如何自动启动 Tor 而不必从我的应用程序中启动它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.