python - 使用 Python 从控制台捕获信息
问题描述
我正在创建一个脚本,我正在尝试专门从网站上抓取 m4a 文件。我目前正在为此目的使用 BS4 和 selenium。
我在获取信息时遇到了一些问题。文件链接不在页面的 HTML 源中。相反,我只能在控制台中找到它。我想要获取的链接在这张图片中(https://imgur.com/a/DLwcE0p),标有“audio_url_m4a:”。
这是我正在使用的一些示例代码:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'browser':'ALL ' }
driver = webdriver.Chrome(r'chromedriver path', desired_capabilities = d)
~~lots of code doing other things not relevant to the post~~
for URL in audm_URL: #this is referencing a line of code where I construct a list of URLs
driver.get(audm)
time.sleep(3)
for entry in driver.get_log('browser'):
print(entry)
这是我得到的输出:
{'level': 'SEVERE', 'message': 'https://audm.herokuapp.com/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)', 'source': 'network', 'timestamp': 1611291689357}
{'level': 'SEVERE', 'message': 'https://cdn.segment.com/analytics.js/v1/5DOhLj2nIgYtQeSfn9YF5gpAiPqRtWSc/analytics.min.js - Failed to load resource: net::ERR_NAME_NOT_RESOLVED', 'source': 'network', 'timestamp': 1611291689357}
大多数与从控制台抓取东西有关的问题都指向我抓取日志,但似乎没有什么能让我知道如何抓取其他变量。有任何想法吗?
这是我想从中获取文件的随机音频页面的链接: https ://audm.herokuapp.com/player-embed?pub=newyorker&articleID=5fe0b9b09fabedf20ec1f70c
感谢大家!
解决方案
driver.get(
"https://audm.herokuapp.com/player-embed?pub=newyorker&articleID=5fe0b9b09fabedf20ec1f70c")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"button"))).click()
src=WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".react-player video"))).get_attribute("src")
print(src)
如果你只是想获得 src 你可以使用上面的代码。
你需要导入
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
如果您想通过控制台日志获取它,请使用:它似乎只适用于无头我正在调查:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
capabilities = webdriver.DesiredCapabilities().CHROME.copy()
capabilities['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Chrome(options=options,desired_capabilities=capabilities)
driver.maximize_window()
time.sleep(3)
driver.get(
"https://audm.herokuapp.com/player-embed?pub=newyorker&articleID=5fe0b9b09fabedf20ec1f70c")
for entry in driver.get_log('browser'):
print(entry)
更新
在无头模式下,w3c 是错误的,因此它正在工作,
对于非无头模式,您必须使用:
options.add_experimental_option('w3c', False)
推荐阅读
- database - 从 codeigniter 网站更新本地和 Web 数据库
- gremlin - AWS Neptune - gremlin 属性顺序
- php - 我的 Laravel 网站的元关键字问题
- ios - Firebase 控制台中未显示崩溃
- java - 在构造函数注释中使用 builder(lombok) 时出错
- xml - Header 和 Page1 之间有更多空间,但从 page2 开始是正确的
- html - 如何仅使用 CSS 使内容在滚动条下?
- ios - 未在新版本的 testflight 中显示内部测试人员的更新
- python - 限制scrapy爬虫的页面深度
- glsl - TouchDesigner 的构造函数的参数太多