![](/img/trans.png)
[英]Can't capture HAR using Python Selenium Script with BrowserMob-Proxy
[英]python selenium browsermob-proxy and interceptor
我將python與selenium和browsermob-proxy一起使用。 發生操作(點擊,提交)后,我希望更改http請求的網址。 我不明白browsermob-proxy的解釋:
request_interceptor(js)[source] Executes the java/js code against each response HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo are available objects to interact with. :param str js: the js/java code to execute
server = Server("C:\\\\browsermob-proxy-2.1.4\\\\bin\\\\browsermob-proxy.bat") server.start() proxy = server.create_proxy() request_js = " *** code *** " proxy.request_interceptor(request_js)
如何在“ request_js”中編寫代碼以更改請求網址? 這有可能嗎?
如何在python中翻譯以下代碼?
proxy.addRequestFilter(new RequestFilter() { @Override public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) { if (messageInfo.getOriginalUri().endsWith("/some-endpoint-to-intercept")) { // retrieve the existing message contents as a String or, for binary contents, as a byte[] String messageContents = contents.getTextContents(); // do some manipulation of the contents String newContents = messageContents.replaceAll("original-string", "my-modified-string"); //[...] // replace the existing content by calling setTextContents() or setBinaryContents() contents.setTextContents(newContents); } // in the request filter, you can return an HttpResponse object to "short-circuit" the request return null; } });
使用硒,您可以使用webdriver與瀏覽器進行交互。 我不確定你的意思。 您的意思是:什么時候發生由代碼引起的動作?
如果是這樣,則解決方案非常簡單:
from selenium import webdriver
driver = webdriver.Firefox() # or Chrome or whatever
def redirect(url, *args, **kwargs):
print("Calling click funtion")
driver.click(*args, **kwargs)
print("Redirecting browser")
webdriver.get(url)
# Eventually do something else.
# Instead of calling driver.click() call this function
driver.event_and_redirect = redirect
還是您的意思是:當用戶在該瀏覽器上引起的操作發生時?
如果是這樣,您可以通過三種方式管理它:
from selenium import webdriver
import time
driver = webdriver.Chrome()
your_url = 'https://www.domain.example'
previous_url = ''
while True:
url = driver.current_url
if url != previous_url:
print(f'Request detected. Redirecting to {your_url}')
driver.get(your_url)
from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from haralyzer import HarParser
import time
server = Server(your_server_path)
server.start()
proxy = server.create_proxy()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'--proxy-server={proxy.proxy}')
driver = webdriver.Chrome(chrome_options=chrome_options)
# Listend for new requests.
while True:
proxy.new_har(self._get_current_time(), options=options)
time.sleep(0.1)
entries = HarParser(self.proxy.har).har_data['entries']
if entries != prev_entries:
print("Requests detected")
# **You may easily save and modify the har file.**
# Scrape the main url in the har.
previous_url = entries[0]['request']['url']
driver.get(another_url)
prev_entries = entries
import win32api
from selenium import webdriver
import time
driver = webdriver.Chrome()
your_url = '' # Url for redirect
width = win32api.GetSystemMetrics(0)
height = win32api.GetSystemMetrics(1)
midWidth = int((width + 1) / 2)
midHeight = int((height + 1) / 2)
state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128
while True:
a = win32api.GetKeyState(0x01)
if a != state_left: # Button state changed
state_left = a
print(a)
if a < 0:
print('Left Button Pressed. Redirecting browser')
driver.get(your_url)
else:
# Left button release
pass
win32api.SetCursorPos((midWidth, midHeight))
time.sleep(0.001)
我希望這可以幫助你。 告訴我是否我誤會了或者您需要澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.