2017-11-10 9 views
1

(HTML 테이블의) 과거 일일 데이터에 대해 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()) 

답변

1

사용 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 
+0

와우! 고맙습니다. – helloworldlevel

+0

내 대답이 도움이 되었다면, [수락] (http://meta.stackexchange.com/a/5235/295067)을 잊지 마세요. 답변 옆에있는 체크 표시 ('✓')를 클릭하여 토글합니다. 회색으로 채워져있었습니다. 감사합니다. – jezrael