我的博客:acsec.xyz 微信公众号: Ac sec
一.概述
我们有时候在爬取jsp动态网站时,发现爬取的数据是不完整的。这时候我们就要获取浏览器F12里面的Network数据包,这样才能得到完整的数据。下面我们用python的Selenium模块配合Browsermob-Proxy抓取Network数据包。
二.环境
1.python3.10
2.pycharm2021.1
3.Browsermob-Proxy2.1.4
下载地址:
https://github.com/lightbody/browsermob-proxy/releases/tag/browsermob-proxy-2.1.4
4.Chrome和对应版本的chromedriver.exe
三.爬取
1.使用百度搜索python,查看network包

2.我们一般是要拿url和response部分


3.我们上代码。
import time
import json
import re
import requests
from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
BMPserver = Server(r'.\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat')
BMPserver.start()
BMPproxy = BMPserver.create_proxy()
chrome_options = Options()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--proxy-server={}'.format(BMPproxy.proxy))
brosver = webdriver.Chrome(options=chrome_options)
url = "https://www.baidu.com/"
BMPproxy.new_har("video",options={'captureContent': True,'captureContent': True})
brosver.get(url)
brosver.find_element_by_xpath('//*[@id="kw"]').send_keys("python")
brosver.find_element_by_xpath('//*[@id="su"]').click()
json_data = BMPproxy.har
for entry in json_data['log']['entries']:
entry_url = entry['request']['url']
print(entry_url)
main()
? 这里是通过browsermob-proxy进行代理,抓取network数据包,工作原理和fiddler差不多。抓到的数据会以json格式输出,然后通过循环把需要的内容(url、response)打印出来
4.json文件格式 

5.上面代码输出结果

6.同理,改变参数可以获取其他数据。在这里我们会发现response部分进行了编码,但是不影响,当你用python打印出来的时候,它会自动解码。
|