python - 无法使用 Python 加载 iframe 的内容
问题描述
我需要使用 python 抓取 iframe 的内容。
当网页加载时,它会提交一个请求并在响应中获取 iframe 的内容。当我使用 BeautifulSoup 来获取数据时,它只会给出初始的空白 iframe 内容。
有什么办法可以获取内容吗?如果是这样,我该怎么做?
这是我的代码:
import requests
from bs4 import BeautifulSoup
page = requests.get("https://etherscan.io/token/0x168296bb09e24a88805cb9c33356536b980d3fc5#balances" + "/token/generic-tokenholders2?a=0x168296bb09e24a88805cb9c33356536b980d3fc5&s=100000000000000000", headers={"content-type":"type"}, timeout=5)
soup = BeautifulSoup(page.content, "html.parser")
balances = soup.find(id="balances")
print(balances.prettify())
table_items = balances.find_all('tr') #I'm trying to collect all the <tr> tags inside an <iframe>
print(table_items) #It shows an empty list because the <iframe> didn't load
解决方案
您必须切换到 iframe,才能从中获取内容。找到 iframe,然后提出新的请求。
iframe_content = requests.get(soup.find("iframe_name")["src"])
看到这个 问题
推荐阅读
- sqlite - SQLite 在内存数据库中是否具有自动删除功能?
- sql - 更新语句不更新数据库记录
- ios - 如何从谷歌网络应用程序上的 safari 修复图像“无法加载资源:HTTP 重定向过多”
- php - 前端控制器重写 htaccess
- mysql - 左连接中连接字段上的“位置”
- azure - add-kdsrootkey 错误 请求不被支持
- excel - 是否有替代公式或加载项可以取消透视 Excel Power Query 等表格?
- android - 在去抖动超时之前调用 RxSeekBar onNext(非常不一致)
- scala - 当元素为空时将 None 分配给选项
- php - Symfony 3 - 如何在树枝中恢复和处理对象?