【问题标题】:Change HTML Code with BeautifulSoup and "reload" like developer tools in Chrome使用 BeautifulSoup 更改 HTML 代码并像 Chrome 中的开发人员工具一样“重新加载”
【发布时间】:2017-01-12 18:52:22
【问题描述】:

假设我有一个 html 网站,其中包含来自您选择的视频托管商的嵌入式视频。

代码看起来像这样(YouTube):

<iframe width="640" height="360"
src="https://www.youtube.com/embed/Some_Video" frameborder="0"
allowfullscreen></iframe>

当我进入 Chrome 开发人员工具时,我可以检查 HTML 代码并更改某些内容并立即看到结果。现在我想将 YouTube 视频链接更改为其他视频链接,例如:

<iframe width="640" height="360"
src="https://www.youtube.com/embed/Some_Other_Video" frameborder="0"
allowfullscreen></iframe>

并想检查发生了什么。例如检查标题或其他内容。如何使用 python 和 BeautifulSoup 自动执行此操作?在网站上执行此操作对我来说至关重要,而不是直接使用浏览器访问链接。

编辑:让我澄清一下我的问题。这就是我要逐步归档的内容: 1. 我想在 iframe 中获取 html 代码 2. 我想将 iframe 的“src”属性更改为不同的东西 3.编辑的html-code必须刷新 4. 我想在 iframe 中提取新的 html 代码

【问题讨论】:

  • 在您的网站上这样做很重要吗?不直接从 youtube 获取信息有什么意义?无论如何,您的 iframe 只会将您重定向到 youtube...
  • YouTube 只是一个简单的例子。这很重要,因为如果您通过网站访问内容,则存在检查。

标签: python beautifulsoup


【解决方案1】:

您可以使用 Selenium 从远程自动化浏览器。进行更改并通过电子邮件或其他方式发送 chrome 控制台文件。下面是一些内容丰富的教程;

https://automatetheboringstuff.com/chapter11/

找到“使用 selenium 模块控制浏览器”

【讨论】:

  • 找不到更改 HMTL 代码并重新检查某些更改的选项?
【解决方案2】:
import bs4
html = '''<iframe width="640" height="360"
src="https://www.youtube.com/embed/Some_Video" frameborder="0"
allowfullscreen></iframe>'''
soup = bs4.BeautifulSoup(html, 'lxml')
soup.iframe['src']="https://www.youtube.com/embed/Some_Other_Video"

出来:

<iframe allowfullscreen="" frameborder="0" height="360" src="https://www.youtube.com/embed/Some_Video" width="640"></iframe>

您可以通过像 dict 一样访问标签来更改标签的属性,但我不知道为什么要在 iframe 中获取 HTML 代码。 这是我能做到的。

【讨论】:

  • 我想要 iframe 中的代码,因为我可以通过 CDN 找到文件的链接。
猜你喜欢
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
  • 2011-04-27
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多