【问题标题】:Can't get src from iframe with beautifulSoup python无法使用 beautifulSoup python 从 iframe 获取 src
【发布时间】:2021-05-17 07:39:29
【问题描述】:

我正在尝试使用 Python 中的 BeautifulSoup 从网页中提取视频,但遇到了一些问题。

当我进入网页并检查 html 元素时,我看到了这个标签

<iframe id="iframe-embed2" src="https://player.voxzer.org/view/1167612b04f6855ecc4bb5e0" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" width="100%" height="auto" frameborder="0"></iframe>

当我复制 src 并打开它时,它会显示视频。

但是当我使用 BeautifulSoup 从网页中查找 iframe 时,我将 src 作为空字符串。

import requests
from bs4 import BeautifulSoup

site = requests.get("the url ...")
soup = BeautifulSoup(site.text, "html.parser")
print(soup.find_all("iframe"))

>>> [<iframe allowfullscreen="true" frameborder="0" height="auto" id="iframe-embed2" mozallowfullscreen="true" scrolling="no" src="" webkitallowfullscreen="true" width="100%"></iframe>]

这里有什么问题?

这个问题没有任何可行的解决方案

Parse iframe with blank src using bs4

【问题讨论】:

  • 可以发网页吗?这样可以更轻松地重现您的问题。
  • 请检查您获取的内容中是否设置了src,即在site = requests... 行之后立即执行print('src="https://player.voxzer.org/view/1167612b04f6855ecc4bb5e0"' in site.text) 并写入输出内容
  • @Daweo 它返回 True

标签: python python-3.x beautifulsoup python-requests


【解决方案1】:

这里有什么问题?

看了site.text,发现https://player.voxzer.org/view/1167612b04f6855ecc4bb5e0要排成一行

mainvideos.push('https://player.voxzer.org/view/1167612b04f6855ecc4bb5e0')

因为.push 是JavaScript 方法,显然这个iframe 的src 是由JavaScript 代码设置的,所以你需要方法来执行站点的JavaScript 代码(例如使用Selenium)。

【讨论】:

    猜你喜欢
    • 2020-09-14
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多