r/learnpython • u/Hour-Inspection-3985 • Aug 31 '23
Web scraping with BeautifulSoup
I'm attempting to scrape the website below. I basically want to create a dataframe in python that looks like the table on the page.
https://www.eex.com/en/market-data/environmentals/futures
So far I have the following code:
url= 'https://www.eex.com/en/market-data/environmentals/futures'r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
table = soup.find_all()
print(table)
I am however struggling to know what to put into the find_all() to pull back the values I need. For example, if I inspect the first settlement price, the html for that object is:
<td style="text-align: right;">86.24</td>
however if I do:
table = soup.find_all(td, style="text-align: right;")
print(table)it pulls nothing back.
Would appreciate some guidance on this, I've tried watching tutorials online which I felt I understood until it came to doing my own example!
Thanks
•
u/duskharborings 50m ago
you forgot quotes around td in your soup call, so python thinks its an undefined variable instead of a string. fixing that still gives you an empty list though because the site builds those pricing tables dynamically via client scripts. standard HTTP fetches only grab the initial blank markup before any scripts execute. open your browser network tab, filter by fetch calls, and grab the hidden backend address sending raw data directly into pandas. if you really must parse rendered cells, playwright or Scrape-fly handles browser rendering without much fuss. grabbing the raw payload straight from their backend endpoint is ten times faster than spinning up headless browsers though.
1
u/JohnnyJordaan Aug 31 '23
If a website uses javascript you won't get far with just a static HTTP client like requests. Look into Selenium or requests-html.