Python 含硒和Tor的旋转IP

Python 含硒和Tor的旋转IP,python,selenium,tor,Python,Selenium,Tor,我有一个selenium配置,用于刮取一个特定的HTTP请求,该请求仅在我单击网站的特定元素时发送。 这就是我使用硒的原因。。。找不到别的办法 我必须更新我的IP,每次我想刮取这个特定的HTTP请求。 为了实现这一点,我使用Tor。 当我开始我的python脚本时,它工作得非常好,设置了一个新的ip并获得了我想要的。 我在脚本中添加了一个try/catch,如果我的脚本第一次无法工作,它将重试10次 问题是当我的脚本再次尝试时,IP无法再旋转 如何做到这一点 导入时间 从随机导入randint

我有一个selenium配置,用于刮取一个特定的HTTP请求,该请求仅在我单击网站的特定元素时发送。 这就是我使用硒的原因。。。找不到别的办法

我必须更新我的IP,每次我想刮取这个特定的HTTP请求。 为了实现这一点,我使用Tor。 当我开始我的python脚本时,它工作得非常好,设置了一个新的ip并获得了我想要的。 我在脚本中添加了一个try/catch,如果我的脚本第一次无法工作,它将重试10次

问题是当我的脚本再次尝试时,IP无法再旋转

如何做到这一点


导入时间
从随机导入randint
从时间上导入睡眠
导入操作系统
导入子流程
从selenium.webdriver.firefox.firefox\u配置文件导入FirefoxProfile
从seleniumwire导入webdriver
从selenium.webdriver.firefox.options导入选项
从fake_useragent导入useragent
选项\u导线={
“代理”:{
'http':'http://localhost:8088',
“https”:”https://localhost:8088',
“无代理”:”
}
}
def firefox_init():
操作系统(“killall tor”)
时间。睡眠(1)
ua=UserAgent()
用户\代理=ua.random
subprocess.Popen(((“tor--HTTPTunnelPort 8088”),shell=True)
时间。睡眠(2)
返回用户\u代理
def profile_firefox():
profile=FirefoxProfile()
profile.set_首选项('permissions.default.image',2)
profile.set_首选项('dom.ipc.plugins.enabled.libflashplayer.so','false')
profile.set_首选项(“general.useragent.override”,firefox_init())
profile.set_首选项(“driver.privatebrowsing.autostart”,True)
profile.update_preferences()
返回配置文件
def options_firefox():
选项=选项()
options.headless=False
返回选项
def firefox_关闭(驱动程序):
driver.quit()
时间。睡眠(3)
操作系统(“killall tor”)
def headless(url):
对于范围(0,10)内的x:
profile=profile_firefox()
options=options\u firefox()
driver=webdriver.Firefox(seleniumwire\u options=options\u wire,Firefox\u profile=profile,options=options,executable\u path='******/headless\u browser/geckodriver')
驱动程序。设置车窗位置(0,0)
驱动程序。设置窗口大小(randint(10242060),randint(10244100))
#睡眠时间(randint(3,10))
获取驱动程序(url)
睡眠时间(randint(3,8))
尝试:
if driver.find_element_by_xpath(“/*[@id=\“********”]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button”):
#驱动程序。通过xpath(“//*[@id=\”*********\“]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button”)查找元素。单击()
#睡眠时间(randint(8,10))
驱动程序。通过xpath(“//*[@id=\”*********\“]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button”)查找元素。单击()
睡眠时间(randint(3,6))
对于driver.requests中的请求:
如果request.path==”https://api.*********.***/*******/*********":
请求\ api=请求
raw=str(请求\ api.body)
请求_api=原始分割(('b\''))
有效负载\u原始=请求\u api[1]
有效载荷=有效载荷_原始[:-1]
如果有效载荷:
header=request.headers
打印(标题、有效负载)
打破
其他:
持续
打破
除:
firefox\u关闭(驱动程序)
时间。睡眠(5)
最后:
firefox\u关闭(驱动程序)
返回收割台,有效载荷
url=”https://check.torproject.org/?lang=fr"
无头(url)

谢谢

好吧,我不可能知道它怎么会不更新IP地址,因为你杀死了tor进程。即使您将tor作为一项服务放在Systemd中,它也会在您重新启动服务时更新。但我可以给你一些指示:

  • 在fake agent模块上,尝试禁用缓存以避免在/tmp目录中缓存或使用托管缓存服务器:

ua=UserAgent(缓存=False,使用\u缓存\u服务器=False)
  • 将Tor放在systemd上,避免使用os.system(),因为它不安全,而且当您直接在脚本上输入系统命令时,它会有很多缺陷。使用该服务文件,您可以重新启动该服务以续订您的IP地址。您可能希望使用Arch Linux Wiki引用在中配置您自己的TOR环境
为了实现这一点,我使用了另一个代理,selenium wire非常好,但需要修复

我已经使用了Browsermob代理并设置了一个上游代理。 结果是,您可以捕获每个HTTP重新请求或响应,每次解析它和ip旋转,并使用tor HTTPTunnelPort配置

proxy_params={'httpProxy':'localhost:8088','httpsProxy':'localhost:8088'}
proxy\u b=server.create\u proxy(params=proxy\u params)

谢谢

谢谢卡洛斯的回答。在我的多重测试中,我认为问题在于seleniumwire包本身。当我导入正式的selenium webdriver时,Tor可以每次更新IP,selenium每次都使用新的IP。我在包中查找当webdriver被杀死时连接是否关闭。