原创 

python3 selenium chromedriver获取当前页面请求的所有URL 代码示例干货demo

分类:python,selenium    750人阅读    IT小君  2021-08-22 21:46

BrowserMob Proxy是什么

官方解释:BrowserMob 代理基于Selenium 开源项目中开发的技术和商业负载测试和监控服务,它可以捕获 Web 应用程序的性能数据(通过HAR 格式),以及操纵浏览器行为和流量,例如将内容列入白名单和黑名单、模拟网络流量和延迟以及重写 HTTP 请求和响应。

BrowserMob Proxy允许您操作HTTP请求和响应,捕获HTTP内容,并将性能数据导出为HAR文件。 BMP作为独立的代理服务器运行良好,嵌入Selenium测试时尤其有用。官网地址:http://bmp.lightbody.net/

怎么做

第一步:官网下载browsermob-proxy服务器代码, 下载好之解压,其中bin目录找到 browsermob-proxy(这个是Linux或者Mac运行文件)或者browsermob-proxy.bat(这个是Windows运行文件)。呆会代码中需要设置它的路径


第二步:安装python3调用 browsermob-proxy的库,命令如下:

pip3 install browsermob-proxy

第三步:编写代码,如下:

from browsermobproxy import Server
from selenium import webdriver

# Purpose of this script: List all resources (URLs) that
# Chrome downloads when visiting some page.

### OPTIONS ###
url = "http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464"
chromedriver_location = r'F:\Program Files (x86)\webdriver\chromedriver.exe'
browsermobproxy_location = r"F:\browsermob-proxy-2.1.4\bin\browsermob-proxy" # location of the browsermob-proxy binary file (that starts a server)
###############

# Start browsermob proxy
server = Server(browsermobproxy_location)
server.start()
proxy = server.create_proxy()

# Setup Chrome webdriver - note: does not seem to work with headless On
options = webdriver.ChromeOptions()
# options.binary_location = chrome_location
# Setup proxy to point to our browsermob so that it can track requests
options.add_argument('--proxy-server=%s' % proxy.proxy)
options.add_argument('--headless')
driver = webdriver.Chrome(chromedriver_location, chrome_options=options)

# Now load some page
proxy.new_har("Example")
driver.get(url)

# Print all URLs that were requested
entries = proxy.har['log']["entries"]
for entry in entries:
    if 'request' in entry.keys():
        print(entry['request']['url'])

server.stop()
driver.quit()


运行结果:

http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/css/index.css
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/js/rotation.js
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/header.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/focus1.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/focus2.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/focus3.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still1.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/focus4.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still12.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still5.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still7.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still10.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still9.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still8.jpg
http://view.jqueryfuns.com/2021/8/13/01cf9937ab5612f2fd1f3379b8d0e464/images/still11.jpg















点击广告,支持我们为你提供更好的服务

canvas炫酷鼠标移动文字粒子特效

中小型创意设计服务公司网站模板

HTML5数字产品服务公司网站模板

小众时尚单品在线电子商务网站模板

现代时尚家具公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

响应式时尚单品在线商城网站模板

有机水果蔬菜HTML5网站模板

响应式咖啡饮品宣传网站模板

html5图标下拉搜索框自动匹配代码

HTML5现代家居装潢公司网站模板

js+css3抽奖转盘旋转点餐代码

html5 canvas彩色碎片组合球形旋转动画特效

网页设计开发公司网站模板

响应式太阳能能源公司网站模板

css+js实现的颜色渐变数字时钟动画特效

html5 canvas进度条圆环图表统计动画特效

css鼠标跟随文字模糊特效

html5 svg夜空中星星流星动画场景特效

jQuery右端悬浮带返回顶部特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务