Issue
I need to get only x
and y
from this soup's return, how can I do that, I tried using split()
and del()
in the string but it didn't work. Here's my code:
import urllib.request
from bs4 import BeautifulSoup
uri = 'https://www.comprasparaguai.com.br/notebook-apple-macbook-pro-2022-apple-m2-memoria-8gb-ssd-256gb-133_42393/'
page = urllib.request.urlopen(uri)
soup = BeautifulSoup(page, 'html.parser')
historico = soup.find(class_='chart-container')
print(historico)
My return:
['<div class="chart-container">\n<canvas data-historico="[{\'y\': 1409.0', " 'x': '07/2022'}", " {'y': 1235.0", " 'x': '08/2022'}", " {'y': 1150.0", " 'x': '09/2022'}", " {'y': 1187.0", " 'x': '10/2022'}", " {'y': 1187.0", ' \'x\': \'10/2022\'}]" id="grafico-modelo"></canvas>\n</div>']
How can I get x
and y
so I can build a graph?
Solution
To get the data as pandas DataFrame you can do:
import pandas as pd
from ast import literal_eval
data = literal_eval(historico.canvas["data-historico"])
# uncomment this to print all data
# print(data)
df = pd.DataFrame(data)
print(df)
Prints:
y x
0 1409.0 07/2022
1 1235.0 08/2022
2 1150.0 09/2022
3 1187.0 10/2022
4 1187.0 10/2022
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.