I'm trying for a school project to scrape data from the following website: https://www.coingecko.com/en/coins/bitcoin/historical_data/usd?start_date=2021-01-01&end_date=2021-09-30. My aim is to get separate lists for the following columns: close, open, volume and date. My problem is, that for the columns volume, open and close, the class name (text-center) is the same (example for the first row):
<th scope="row" class="font-semibold text-center">2021-09-30</th>
<td class="text-center">
$782,626,384,092
</td>
<td class="text-center">
$30,068,690,312
</td>
<td class="text-center">
$41,588
</td>
<td class="text-center">
N/A
</td>
I tried to solve it with the following code but wasn't successful (for the close values):
from bs4 import BeautifulSoup
import requests
import pandas as pd
website = 'https://www.coingecko.com/en/coins/bitcoin/historical_data/usd?start_date=2021-01-01&end_date=2021-09-30#panel'
response = requests.get(website)
soup = BeautifulSoup(response.content, 'html.parser')
results = soup.find('table', {'class':'table-striped'}).find('tbody').find_all('tr')
close = []
volume = []
open = []
date = []
all_tr = soup.find_all('tr')
print('rows:', len(all_tr))
for row in all_tr:
all_td = row.find_all('td', recursive=False)
print('columns:', len(all_td))
for column in all_td:
print(column.text)
close.append(all_td[4].text)
If somebody could help me out, I would be very grateful!