selenium3.0不用代理的情况下,获取异步请求的数据
import json
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
def get_xhr_logs(chrome):
log_xhr_array = []
for typelog in chrome.log_types:
perfs = chrome.get_log(typelog)
for row in perfs:
log_data = row
message_ = log_data['message']
try:
log_json = json.loads(message_)
log = log_json['message']
if log['method'] == 'Network.responseReceived':
# 去掉静态js、css等,仅保留xhr请求
type_ = log['params']['type']
if type_ == "XHR":
log_xhr_array.append(log)
except:
pass
return log_xhr_array
def get_log_options():
option = webdriver.ChromeOptions()
option.add_argument('--no-sandbox')
option.add_argument('--headless')
option.add_argument("--disable-extensions")
option.add_argument("--allow-running-insecure-content")
option.add_argument("--ignore-certificate-errors")
option.add_argument("--disable-single-click-autofill")
option.add_argument("--disable-autofill-keyboard-accessory-view[8]")
option.add_argument("--disable-full-form-autofill-ios")
option.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:55.0) Gecko/20100101 Firefox/55.0')
option.add_experimental_option('w3c', False)
option.add_experimental_option('perfLoggingPrefs', {
'enableNetwork': True,
'enablePage': False,
})
return option
def get_caps():
caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {
'browser': 'ALL',
'performance': 'ALL',
}
caps['perfLoggingPrefs'] = {
'enableNetwork': True,
'enablePage': False,
'enableTimeline': False
}
return caps
import json
import os
import time
if __name__ == '__main__':
# 使用工具类来获取options配置,而不是平时的webdriver.ChromeOptions()方法
options = get_log_options()
# 使用工具类来获取caps
desired_capabilities = get_caps()
# 这里也可以对options和caps加入其他的参数,比如代理参数等
chrome = webdriver.Chrome(options=options, desired_capabilities=desired_capabilities)
chrome.get("https://www.baidu.com/")
chrome.maximize_window()
# 用工具类来获取ajax请求日志
logs = get_xhr_logs(chrome)
for log in logs:
print(log)
chrome.quit()
把你的66行到70行换成这个试试,
options = webdriver.ChromeOptions()
desired_capabilities = options.to_capabilities()
desired_capabilities['proxy'] = {
"httpProxy": None,
"ftpProxy": None,
"sslProxy": None,
"noProxy": None,
"proxyType": "MANUAL",
"class": "org.openqa.selenium.Proxy",
"autodetect": False
}
driver = webdriver.Chrome(desired_capabilities = desired_capabilities)