问题描述
我正在尝试使用 tor 浏览器,并为我在 python 中访问的每个 URL 获取一个新的 IP 地址.我可以打开一个运行 Tor 浏览器的 selenium 实例,但是如何为我访问的每个网站请求一个新 IP?
I am trying to use tor browser, and get a new IP address each URL I visit in python. I am able to open an instance of selenium running the tor browser, but how can I request a new IP every website I visit?
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)
browser = None
def get_browser(binary=None):
browser = webdriver.Firefox(firefox_binary=binary)
return browser
if __name__ == "__main__":
browser = get_browser(binary=firefox_binary)
urls = (
('tor browser check', 'https://check.torproject.org/'),
('ip checker', 'http://icanhazip.com')
)
for url_name, url in urls:
print "getting", url_name, "at", url
browser.get(url)
推荐答案
要使用 Python 为每个请求请求一个新 IP,您需要打开一个到 ControlPort 并发出 NEWNYM 信号.
To use Python to request a new IP for every request, you need to open a connection to the ControlPort and issue a NEWNYM signal.
您可以使用 Stem 来简化连接和命令:
You can use Stem to simplify the connection and commands:
from stem.control import Controller
from stem import Signal
if __name__ == '__main__':
with Controller.from_port(port = 9051) as controller:
controller.authenticate('password') # provide the password here if you set one
controller.signal(Signal.NEWNYM) # switch to clean circuits
请记住,Tor 可能会限制 NEWNYM
请求的速率,因此您可能需要等待片刻(默认为 10 秒)才能发出该命令.此外,由于出口节点数量有限,您的电路可能会获得相同的出口节点,具体取决于您发出的请求数量.
Keep in mind Tor may rate limit NEWNYM
requests so you may need to wait a short while (default 10 seconds) before issuing that command. Also, due to the limited number of exit nodes, your circuits might get the same exit node depending on how many requests you are issuing.
每次要获取新 IP(交换电路)时都需要发出此命令.
You need to issue this command every time you want to get a new IP (switch circuits).
这篇关于Tor浏览器,新IP不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!