gpt4 book ai didi

python - 尝试抓取 HTML 表并转换为 Python 中的数据框。代码无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:51 24 4
gpt4 key购买 nike

我正在尝试抓取 coinmarketcap 网站的历史每日数据(在 HTML 表中),但没有得到正确的结果。下面是代码。该代码仅返回表的最后一行。我在循环中做错了什么...非常感谢任何帮助!

import requests
from bs4 import BeautifulSoup
import pandas as pd

data = requests.get('https://coinmarketcap.com/currencies/ethereum/historical-data')

soup = BeautifulSoup(data._content, 'lxml')

table = soup.find_all('table')[0]

#the table has 7 columns, about 30 rows
new_table = pd.DataFrame(columns=range(0,7), index = [0])

row_marker = 0
for row in table.find_all('tr'):
column_marker = 0
columns = row.find_all('td')
for column in columns:
new_table.iat[row_marker,column_marker] = column.get_text()
column_marker += 1
print (new_table.head())

最佳答案

使用read_html :

url = 'https://coinmarketcap.com/currencies/ethereum/historical-data'

df = pd.read_html(url, parse_dates=[0])[0]
print (df.head())
Date Open High Low Close Volume Market Cap
0 2017-11-09 308.64 329.45 307.06 320.88 893250000 29509000000
1 2017-11-08 294.27 318.70 293.10 309.07 967956000 28128700000
2 2017-11-07 298.57 304.84 290.77 294.66 540766000 28533300000
3 2017-11-06 296.43 305.42 293.72 298.89 579359000 28322700000
4 2017-11-05 300.04 301.37 295.12 296.26 337658000 28661500000

关于python - 尝试抓取 HTML 表并转换为 Python 中的数据框。代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47218100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com