试图在雅虎财经“世界指数”排行榜上抢占一席之地

2024-03-28 17:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用下面的代码将雅虎财经(https://finance.yahoo.com/world-indices/)中的“世界索引”列表刮到一个数据框架中。但是,当我收到以下信息时:

ValueError:未找到任何表

import pandas as pd
major_indices = pd.read_html("https://finance.yahoo.com/world-indices/")
df = pd.read_html(driver.find_element_by_id("history_table").get_attribute('outerHTML'))[0]
df.head()

Output:
ValueError: No tables found

我试着看了看这一页,发现桌子周围都是人


<tbody data-reactid="36"> ... </tbody>. 

我试图寻找如何进行刮削,但没有任何运气

熊猫数据阅读器是一条路吗?是否有更强大的网页刮板,我应该使用,如硒或美丽的汤


Tags: 数据代码httpscomdfworldreadhtml
1条回答
网友
1楼 · 发布于 2024-03-28 17:35:08

我建议使用Python查看BeautifulSoup以进行web抓取。如果您使用像PyCharm这样的IDE,浏览BeautifulSoup对象以查找网页内容应该不会太困难

下面是您的示例的起点

from bs4 import BeautifulSoup
import requests

website_html = requests.get("https://finance.yahoo.com/world-indices/").text
soup = BeautifulSoup(website_html)
table = soup.find("tbody", {"data-reactid": "36"})
table_text = [[cell.text for cell in row.contents] for row in table.contents]

相关问题 更多 >