简介
爬取chrome中network的详细信息也是因为公司有这样的需求,所以才动手找办法,最后还真被我解决了。chrome中network是非常强大的,但是想要爬取到network中的详情,确实不那么容易,找过各种办法去操纵浏览器,还是没能实现。后来另辟蹊径,利用selenium抓取到了网络请求活动数据。
代码实现原理,调用selenium,让selenium记录网络请求的日志,然后读取selenium网络请求的日志,然后就顺利拿到和chrome中network相同的内容了
上代码
# -*- coding=utf-8 -*-
import json
import time
import datetime
import hashlib
import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from urllib.parse import urlparse
def netloc_info(url):
"""
这个是为了获取url的域名,这个代码对只获取网络请求没用,
"""
res = urlparse(url)
netloc_orig = res.netloc
if str(netloc_orig)[0:3] == "www":
netloc = str(netloc_orig).replace(str(netloc_orig)[0:4], "")
else:
netloc = str(netloc_orig)
return netloc
def info(url):
"""
调用selenium,开启selenium的日志收集功能,收集所有日志,并从中挑出network部分,分析格式化数据,传出
:param url:
:return:
"""
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
chrome_options.add_experimental_option('w3c', False)
caps = {
'browserName': 'chrome',
'loggingPrefs': {
'browser': 'ALL',
'driver': 'ALL',
'performance': 'ALL',
},
'goog:chromeOptions': {
'perfLoggingPrefs': {