【发布时间】:2017-04-08 16:45:32
【问题描述】:
嗨,我一直在尝试在一些抓取网站的请求后更改我的 ip,但是当我运行我在某些博客上找到的这段代码时,任何事情都会发生,我的意思是我的 ip 是相同的在两个输出中。有人知道我做错了什么吗?
from stem import Signal
from stem.control import Controller
import requests
def set_new_ip():
"""Change IP using TOR"""
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='my_password')
controller.signal(Signal.NEWNYM)
local_proxy = '127.0.0.1:8118'
http_proxy = {'http://': local_proxy,
'https://': local_proxy}
current_ip = requests.get(url='http://icanhazip.com/',
proxies=http_proxy,
verify=False)
print(current_ip.text)
set_new_ip()
current_ip = requests.get(url='http://icanhazip.com/',
proxies=http_proxy,
verify=False)
print(current_ip.text)
【问题讨论】: