共計(jì) 830 個(gè)字符,預(yù)計(jì)需要花費(fèi) 3 分鐘才能閱讀完成。
在 Python 中獲取 iframe 頁(yè)面內(nèi)容的方法有兩種:使用 Requests 庫(kù)和使用 Selenium 庫(kù)。
- 使用 Requests 庫(kù):
import requests
url = 'https://example.com'
response = requests.get(url)
html_content = response.text
# 使用 BeautifulSoup 解析 html 內(nèi)容
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
# 找到 iframe 元素
iframe = soup.find('iframe')
# 獲取 iframe 的 src 屬性
iframe_src = iframe['src']
# 獲取 iframe 頁(yè)面內(nèi)容
iframe_response = requests.get(iframe_src)
iframe_content = iframe_response.text
print(iframe_content)
- 使用 Selenium 庫(kù):
from selenium import webdriver
url = 'https://example.com'
driver = webdriver.Chrome()
driver.get(url)
# 切換到 iframe
iframe = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(iframe)
# 獲取 iframe 頁(yè)面內(nèi)容
iframe_content = driver.page_source
print(iframe_content)
# 退出瀏覽器
driver.quit()
以上是兩種常用的方法來(lái)獲取 iframe 頁(yè)面內(nèi)容,可以根據(jù)具體需求選擇適合的方法。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完