![](/img/trans.png)
[英]Python: Requests over Tor , Enabling rotating IP - missing torrc file
[英]Rotating IP with selenium and Tor
我有一個用於抓取特定 HTTP 請求的 selenium 配置,僅當我單擊網站的特定 REACT 元素時才會發送此請求。 這就是我使用硒的原因……找不到其他方法。
每次我想抓取這個特定的 HTTP 請求時,我都必須更新我的 IP。 為此,我使用 Tor。 當我啟動我的 python 腳本時,它運行得很好,Tor 設置了一個新的 ip 並抓取了我想要的東西。 我在我的腳本中添加了一個 try/catch,如果我的腳本第一次不能工作,它會重試 10 次。
問題是當我的腳本再試一次時,IP 不能再旋轉了....
如何實現這一目標?
import time
from random import randint
from time import sleep
import os
import subprocess
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from seleniumwire import webdriver
from selenium.webdriver.firefox.options import Options
from fake_useragent import UserAgent
options_wire = {
'proxy': {
'http': 'http://localhost:8088',
'https': 'https://localhost:8088',
'no_proxy': ''
}
}
def firefox_init():
os.system("killall tor")
time.sleep(1)
ua = UserAgent()
user_agent = ua.random
subprocess.Popen(("tor --HTTPTunnelPort 8088"),shell=True)
time.sleep(2)
return user_agent
def profile_firefox():
profile = FirefoxProfile()
profile.set_preference('permissions.default.image', 2)
profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
profile.set_preference("general.useragent.override", firefox_init())
profile.set_preference("driver.privatebrowsing.autostart", True)
profile.update_preferences()
return profile
def options_firefox():
options = Options()
options.headless = False
return options
def firefox_closing(driver):
driver.quit()
time.sleep(3)
os.system('killall tor')
def headless(url):
for x in range(0, 10):
profile = profile_firefox()
options = options_firefox()
driver = webdriver.Firefox(seleniumwire_options=options_wire,firefox_profile=profile, options=options, executable_path='******/headless_browser/geckodriver')
driver.set_window_position(0, 0)
driver.set_window_size(randint(1024, 2060), randint(1024, 4100))
# time.sleep(randint(3,10))
driver.get(url)
time.sleep(randint(3,8))
try:
if driver.find_element_by_xpath("//*[@id=\"*******\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button"):
# driver.find_element_by_xpath("//*[@id=\"*******\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
# time.sleep(randint(8,10))
driver.find_element_by_xpath("//*[@id=\"*******\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
time.sleep(randint(3,6))
for request in driver.requests:
if request.path == "https://api.*********.***/*******/*********":
request_api = request
raw = str(request_api.body)
request_api = raw.split(('b\''))
payload_raw = request_api[1]
payload = payload_raw[:-1]
if payload:
header = request.headers
print(header, payload)
break
else:
continue
break
except:
firefox_closing(driver)
time.sleep(5)
finally:
firefox_closing(driver)
return header, payload
url="https://check.torproject.org/?lang=fr"
headless(url)
謝謝
好吧,我不可能知道它是如何不更新 IP 地址的,因為你殺死了 tor 進程。 即使您將 Tor 作為服務放在 Systemd 中,它肯定會在您重新啟動服務時更新。 但我可能會給你一些指導:
ua = UserAgent(cache=False, use_cache_server=False)
所以為了實現這一點,我使用了其他代理,selenium-wire 非常好,但需要修復。
我已經使用 Browsermob 代理並設置了一個上游代理來使用。 結果是您可以捕獲每個 HTTP 請求或響應解析它並且每次都輪換 ip 並使用 HTTPTunnelPort 配置。
proxy_params = {'httpProxy': 'localhost:8088', 'httpsProxy': 'localhost:8088'}
proxy_b = server.create_proxy(params=proxy_params)
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.