首页 > 解决方案 > 无法使用 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

标签: pythonpython-3.xiframeweb-scrapingbeautifulsoup

解决方案


您必须切换到 iframe,才能从中获取内容。找到 iframe,然后提出新的请求。

iframe_content = requests.get(soup.find("iframe_name")["src"])

看到这个 问题


推荐阅读