【问题标题】:Selenium Python - Get a list of all loaded URLs (images, scripts, stylesheets etc)Selenium Python - 获取所有已加载 URL 的列表(图像、脚本、样式表等)
【发布时间】:2018-06-04 10:58:59
【问题描述】:

当谷歌浏览器通过 Selenium 加载网页时,它可能会加载页面所需的其他文件,例如来自<img src="example.com/a.png"><script src="example.com/a.js"> 标签。此外,CSS 文件。

如何获取浏览器加载页面时下载的所有 URL 的列表? (以编程方式,在 Python 中使用 Selenium 和 chromedriver) 也就是Chrome中开发者工具的“网络”标签中显示的文件列表(即显示下载文件的列表)。

使用 Selenium、chromedriver 的示例代码:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/usr/bin/x-www-browser"
driver = webdriver.Chrome("./chromedriver", chrome_options=options)
# Load some page
driver.get("https://example.com")
# Now, how do I see a list of downloaded URLs that took place when loading the page above?

【问题讨论】:

  • 你有什么尝试吗?显示您的代码
  • @Andersson 我并没有真正尝试过获取下载的 URL 列表。但我刚刚编辑了这个问题,将我的 Selenium 设置包含在 Python 中。除了这个,我不知道我还能做什么。
  • @vatsug BrowserMob 代理解决方案不能解决这个问题吗?
  • @GPT14 我现在去看看,回来报告,谢谢你的回复。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您可能想查看 BrowserMob 代理。它可以捕获 Web 应用程序的性能数据(通过 HAR 格式),以及操纵浏览器行为和流量,例如将内容列入白名单和黑名单、模拟网络流量和延迟,以及重写 HTTP 请求和响应。

取自 readthedocs,使用简单,与 selenium webdriver api 集成良好。您可以阅读有关 BMP 的更多信息here

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("google")
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob

server.stop()
driver.quit()

【讨论】:

  • 感谢您的回复@GPT14,您使用 BrowserMob 代理的想法很棒 - 它非常适合我的需求。我修改了您示例中的代码以使用 Google Chrome 并添加了更多代码,以在 stackoverflow.com/a/50681175/6352803 的答案中打印请求的 URL。再次感谢您!
  • 我会在几个小时内接受我自己的答案。您的答案并不是真正的答案,因为它没有列出实际的 URL,我在答案的代码中这样做了。但是,在我的回答中使用 BrowserMob 代理的想法,我已经给了你荣誉。
【解决方案2】:

继续@GPT14 在他的answer 中的建议,我编写了一个小脚本,它完全实现了我想要的并打印了某个页面加载的 URL 列表。

这使用 BrowserMob 代理。非常感谢@GPT14 建议使用它——它非常适合我们的目的。我已经从他的答案中更改了代码,并将其调整为 Google Chrome webdriver 而不是 Firefox。我还扩展了脚本,以便它遍历 HAR JSON 输出并列出所有请求 URL。请记住根据您的需要调整以下选项。

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 = "https://example.com"
chromedriver_location = "./chromedriver" # Path containing the chromedriver
browsermobproxy_location = "/opt/browsermob-proxy-2.1.4/bin/browsermob-proxy" # location of the browsermob-proxy binary file (that starts a server)
chrome_location = "/usr/bin/x-www-browser"
###############

# 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)
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多