selenium库获取浏览器请求链接(Network)

获取浏览器请求的地址,和network差不多一致

根据获取内容进行过滤

import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get('https://www.baidu.com/')

def process_browser_log_entry(entry):
    response = json.loads(entry['message'])['message']
    return response

browser_log = driver.get_log('performance')
print(type(browser_log))
events = [process_browser_log_entry(entry) for entry in browser_log]
events = [event for event in events if 'Network.responseReceived' in event['method']]
def flei(a):
    for i in a:
        if i['method'] == "Network.responseReceived":
            if 'm3u8' in i['params']["response"]['url']:   #在链接处过滤关键词,此处过滤“m3u8”文件
                print(i["params"]['response']['status'], i['params']['response']['mimeType'],i['params']["response"]['url'])
            print(i["params"]['response']['status'], i['params']['response']['mimeType'],i['params']["response"]['url'])
flei(events)
driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': events[0]["params"]["requestId"]}) 

效果图:

selenium库获取浏览器请求链接(Network)_第1张图片

你可能感兴趣的:(selenium,python,chrome)