首页 > 解决方案 > 如何使用 BeautifulSoup 在这个 iframe 中获取 src?

问题描述

这是 iframe:

<iframe allow="autoplay; fullscreen" allowfullscreen="yes" frameborder="no" scrolling="no" src="https://www.example.com" style="width: 100%; height: 100%;"></iframe>

我的尝试:

print(soup.find("iframe")["src"])

此案例的附加信息:

from selenium import webdriver
from bs4 import BeautifulSoup
import time

driver = webdriver.Firefox()

driver.get("https://9anime.to/watch/puella-magi-madoka-magica.q1n/yqq3r9x")
time.sleep(5)
soup = BeautifulSoup(driver.page_source, "lxml")
print(soup.find("iframe"))

driver.quit()

如果您想找到确切的 html,“附加信息”代码将复制/粘贴。(也可以print(soup.prettify())在整个 html 脚本的最后添加。

标签: pythonselenium-webdriverfirefoxiframebeautifulsoup

解决方案


解决方案: (soup.find("iframe")["src"]

就我而言: print((soup.find("iframe"))["src"])


推荐阅读