python - 使用 BeautifulSoup 和 Python 提取 iframe
问题描述
我使用 Canvas LMS,我想从某些页面中提取 iframe 以更改 src 内容。我尝试以下方法:
//some code
soup = BeautifulSoup(page_html, 'html.parser')
pretty_html = soup.prettify()
soup = BeautifulSoup(pretty_html, 'html.parser')
iframe = soup.find('iframe')
print(iframe)
但结果出乎意料,我得到了这个结果:
None
None
<iframe allowfullscreen="" frameborder="0" height="276" mozallowfullscreen="" scrolling="no" src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/%20f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490"></iframe>
None
None
None
None
None
None
我期待只得到这个
<iframe allowfullscreen="" frameborder="0" height="276" mozallowfullscreen="" scrolling="no" src="https://fast.player.liquidplatform.com/pApiv2/embed/e50a2b66dc19adc532f288eb4bf2d302/%20f2c5f6ca3a4610c55d70cb211ef9d977" webkitallowfullscreen="" width="490"></iframe>
收到的页面html只有一个iframe,结果有什么问题?我想我应该只收到一个 iframe 对象,但似乎我收到了一个列表。有人可以为我澄清我做错了什么?
解决方案
我发现如何解决问题。
我更改代码:
iframe = soup.find('iframe')
至
iframe = soup.find_all('iframe')
然后,我没有收到 None 作为响应,而是开始收到 []。一个空值。
我使用以下方法对其进行了测试:
if iframes != [] :
print( iframes[0]['src'] )
我使用 iframes[0]['src'] 获得了 src 的内容
推荐阅读
- java - 为什么按钮对象的行为取决于它的声明位置
- swift - CreateML 标记时出现“执行被中断”错误
- ocaml - 如何安装与 ocaml 4.05.0 版本兼容的 camlp5?
- kubernetes - Kubernetes 端口列表中的端口表示
- javascript - 按其值对 Array of Objects 中的每个 Object 进行排序
- postgresql - 必须至少安装一个 postgresql-client-
包裹 - python - 如何使用 2 个数据框填写表格
- python - 从 SMS 消息中获取参数
- go - 未定义:proto.ProtoPackageIsVersion3
- javascript - $("body").on("drop") 与 Dropzone.js