I am trying to create a DataFrame with the CPI data scraped from a website with Beautiful Soup, sorted by year and Country. The DF would have the following structure:
| country | year | q1 | q2 | q3 | q4 | year |
|---|---|---|---|---|---|---|
| Australia | 2022 | 123.9 | 126.1 | |||
| Australia | 2021 | 117.9 | 118.8 | 119.7 | 117.2 | 119.4 |
| ... |
I am able to get all the needed data from the following script:
import requests
import pandas as pd
from bs4 import BeautifulSoup as bs
list_countries=['australia','canada','brazil','italy','japan','mexico','new-zealand','france','germany','philippines','india','korea','russia','singapore','switzerland','uk','usa']
url = 'https://www.rateinflation.com/consumer-price-index/australia-historical-cpi/'
r = requests.get(url)
soup = bs(r.text, 'html.parser')
cpi_get = soup.find('table', class_='css-8rh80p eyyd7td0')
for year in cpi_get.find_all('tr'):
quarters = year.find_all('td')
print(quarters)
The problem is that the output has the following structure, for each iteration:
[<td>2021</td>, <td>117.9</td>, <td>118.8</td>, <td>119.7</td>, <td>121.3</td>, <td>119.4</td>]
Having the first td as the year, the second q1,... and consecutively up to year. As it does not have any class or extra info apart of the position, I don´t know how to create the table from that.
Does anyone know how to get the desired output from that?