python - python selenium browsermob-proxy 和拦截器
问题描述
我将 python 与 selenium 和 browsermob-proxy 一起使用。当动作发生时(点击、提交),我希望更改 http 请求 url。我不明白 browsermob-proxy 的解释:
request_interceptor(js)[源代码] 针对每个响应 HttpRequest 请求执行 java/js 代码,HttpMessageContents 内容,HttpMessageInfo messageInfo 是可与之交互的可用对象。:param str js: 要执行的 js/java 代码
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”中编写代码以更改请求 URL?这是一个可能的例子吗?
如何在 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;
}
});
解决方案
使用 selenium,您可以使用 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)
我希望这可以帮助你。如果我误解了或者您需要澄清,请告诉我。
推荐阅读
- mysql - 数据库迁移不为空导致 DBeaver 出现问题?
- c# - 逗号分隔、竖线分隔记录的排序列表
- javascript - UnhandledPromiseRejectionWarning:TypeError:message.author.displayAvatarURL 不是函数
- python - 结合 2 个 numpy 数组
- singly-linked-list - 为什么我得到无限循环
- python - SciPy Linprog() 优化
- c++ - 如何捕获由 boost::spirit::x3 解析器解析的值,以在语义动作的主体中使用?
- python - FLASK,Heroku,React CORS 问题
- node.js - 连接节点和 mongo 时出错,因为无法 GET /
- ruby - 如何将我的两种方法组合成一种方法?