Nee*_*eko 2 python django python-3.x selenium-webdriver
我正在启动一个新的 Django 项目,我正在尝试使用 Selenium 捕获网络流量。
我已经使用 Selenium-wire(MITM 代理)实现了这个目标,但是 Django 不喜欢使用 selenium-wire(必须使用“--nothreading --noreload”启动服务器,连接错误...)。我正在寻找使用现代解决方案来实现这一目标,例如直接解析 Firefox 的网络开发工具或使用 Firefox 插件。我正在使用 Firefox Geckodriver 进行测试。
for x in range(0, 10):
profile = profile_firefox()
options = options_firefox()
driver = webdriver.Firefox(firefox_profile=profile, options=options, executable_path='/Users/*****/Desktop/selenium-master/headless_browser/geckodriver')
try:
driver.set_window_position(0, 0)
driver.set_window_size(randint(1024, 2060), randint(1024, 4100))
time.sleep(randint(3,10))
driver.get(url)
wait = ui.WebDriverWait(driver, 10)
time.sleep(randint(8,10))
if driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button"):
driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
del driver.requests
time.sleep(randint(8,10))
driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
time.sleep(randint(10,20))
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
time.sleep(8)
break
except:
print("Houston on a eu un probleme")
firefox_closing(driver)
Run Code Online (Sandbox Code Playgroud)
编辑 :
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('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', 'localhost')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.set_preference("driver.privatebrowsing.autostart", True)
profile.update_preferences()
return profile
Run Code Online (Sandbox Code Playgroud)
使用 Socks、HTTP、SSL 配置进行测试 2:
server = Server('/Users/*****/Desktop/selenium-master/browsermob-proxy-2.1.4/bin/browsermob-proxy')
server.start()
proxy = server.create_proxy()
proxy.selenium_proxy()#Dont understand what it does ???
port = int(proxy.port)
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('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', 'localhost')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference('network.proxy.ssl', 'localhost')
profile.set_preference('network.proxy.ssl_port', port)
profile.set_preference('network.proxy.http', 'localhost')
profile.set_preference('network.proxy.http_port', port)
profile.set_preference('network.proxy.socks_remote_dns', False)
profile.set_preference('driver.privatebrowsing.autostart', True)
profile.update_preferences()
Run Code Online (Sandbox Code Playgroud)
似乎Http代理覆盖了socks配置......
如果您对我的代码或解决方案有任何线索或建议,非常感谢。
小智 6
Python 有一个名为 selenium-wire 的包。您可以使用该包来捕获网络流量并验证它们。selenium-wire 是 selenium 的扩展版本,具有 selenium 的所有功能以及用于捕获网络和验证的额外 API。以下是文章的链接 https://sensoumya94.medium.com/validate-network-requests-using-selenium-and-python-3da5be112f7b
以下是包的存储库
https://github.com/wkeeling/selenium-wire
示例代码 -
from seleniumwire import webdriver # Import from seleniumwire
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# Go to the Google home page
driver.get('https://www.google.com')
# Access requests via the `requests` attribute
for request in driver.requests:
if request.response:
print(
request.url,
request.response.status_code,
request.response.headers['Content-Type']
)
Run Code Online (Sandbox Code Playgroud)
您可以使用代理来捕获网络流量。browsermob-proxy与 Python 中的 selenium 配合得很好。您需要先下载 browsermob 可执行文件。这是 Firefox 的一段代码:
from browsermobproxy import Server
from selenium import webdriver
server = Server('path_to_executable')
server.start()
proxy = server.create_proxy()
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("file_name", options={'captureHeaders': True, 'captureContent': True})
driver.get("your_url")
proxy.wait_for_traffic_to_stop(1, 60)
for ent in proxy.har['log']['entries']:
print(ent)
server.stop()
driver.quit()
Run Code Online (Sandbox Code Playgroud)
Browsermob 是正确的方法。
我必须了解 browsermob 和 tor 的工作原理。对于 Tor,您必须像这样启用 HTTPTunnelPort 配置。
tor --HTTPTunnelPort 8088
Run Code Online (Sandbox Code Playgroud)
并配置 browsermob 来使用它。
proxy_params = {'httpProxy': 'localhost:8088', 'httpsProxy': 'localhost:8088'}
proxy_b = server.create_proxy(params=proxy_params)
Run Code Online (Sandbox Code Playgroud)
谢谢。
| 归档时间: |
|
| 查看次数: |
11421 次 |
| 最近记录: |