如何使用python硒获取浏览器网络日志

时间:2018-11-13 17:49:19

标签: python selenium selenium-webdriver selenium-chromedriver

我正在尝试使用硒来获取浏览器网络日志以调试请求/响应。您能帮我找出办法吗?

我正在使用硒3.14.0和最新的Chrome浏览器。

4 个答案:

答案 0 :(得分:8)

使用python + selenium + firefox

除非必须,否则不要设置代理 - 为了获得出站 API 请求,我使用了这个答案中的解决方案,但在 python 中:https://stackoverflow.com/a/45859018/14244758

test = driver.execute_script("var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;")

for item in test:
  print(item)

你得到了一个字典数组。

这让我可以看到所有的网络请求。我正在使用它从其中一个请求中解析出一个参数,以便我可以使用它来针对 API 发出自己的请求。

答案 1 :(得分:3)

使用Python和ChromeDriver

要获取网络日志,您还需要在Python中安装BrowserMobProxy和硒

    const childrenFilter = (childrenData, filters) => {
    let filteredData = childrenData.filter(item => {
        for (var property in filters) {
            var optionalValues = filters[property];
            var value = item[property];
            if (item.children) {
                item.children = childrenFilter(item.children, filters);
            }
            let hasValue = value == optionalValues;
            if (hasValue || item.children.length) { // include item when children mathes the pattern
                return true;
            }
            return false;
        }
        return false;
    }, this);
    return filteredData;
    }

您需要启动浏览器代理并在chrome驱动程序的chrome选项中配置代理,

pip install browsermob-proxy

然后您可以使用硒导航到页面

from browsermobproxy import Server
from selenium import webdriver

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

# Configure the browser proxy in chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
browser = webdriver.Chrome(chrome_options = chrome_options)

#tag the har(network logs) with a name
proxy.new_har("google")

导航后,您可以从代理获取json格式的网络日志

driver.get("http://www.google.co.in")

在退出驱动程序之前,还要在最后停止代理服务器,

print(proxy.har) # returns a Network logs (HAR) as JSON 

答案 2 :(得分:0)

要仅获取网络日志直到页面加载完成(页面主要使用期间没有ajax /异步网络日志),您可以获取性能日志:http://chromedriver.chromium.org/logging/performance-log

例如,要为ChromeDriver启用性能日志记录,

DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);

chromium performance-log page还链接到此完整示例https://gist.github.com/klepikov/5457750,该示例包含Java和python代码以获取性能日志。

同样,请务必记住,这只会使网络请求生效,直到页面加载完成为止。之后,驱动程序将仅返回相同的性能日志,直到页面重新加载。


如果要在整个页面使用过程中异步获取网络日志,则可以使用BrowserMobProxy充当Selenium驱动程序的代理服务器并捕获所有这些网络请求。然后,您可以从BrowserMobProxy的生成的HAR文件中获取那些捕获的请求:https://github.com/lightbody/browsermob-proxy#using-with-selenium

// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);

// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);

// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

// create a new HAR with the label "yahoo.com"
proxy.newHar("yahoo.com");

// open yahoo.com
driver.get("http://yahoo.com");

// get the HAR data
Har har = proxy.getHar();

一旦有了HAR文件,它就是一个类似JSON的网络事件列表,您可以使用它。

答案 3 :(得分:0)

尝试 selenium-wire,我认为这是一种更好的方法,它也提供了 undetected-chromedriver 以防止机器人检测。

相关问题