无法使用Python加载iframe的内容

时间:2018-04-24 16:08:00

标签: python python-3.x iframe web-scraping beautifulsoup

我需要使用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

1 个答案:

答案 0 :(得分:0)

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

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

请参阅this问题