This is a complete html I work with.
This is simplified version of above HTML:
<table class="premium">
<tr class="retailer top-offer" data-pricer="47.84" saler-id="123">...</td>
<tr class="retailer" data-pricer="57.11" saler-id="234">...</td>
</table>
<table class="basic-supp">
<tr class="retailer top-offer" data-pricer="41.87" saler-id="456">...</td>
<tr class="retailer" data-pricer="58.12" saler-id="567">...</td>
</table>
From TABLE with class="basic-supp" from TR tags and from data-pricer="..." attributes I need to extract values.
I tried this method on simplified html:
from bs4 import BeautifulSoup
with open('file.html', 'r') as f:
contents = f.read()
soup = BeautifulSoup(contents, 'lxml')
tags = soup.find_all('tr')
for tag in tags:
print(tag.attrs['data-pricer'])
> 47.84
> 57.11
> 41.87
> 58.12
This is almost what I need, except the fact it takes values from both tables instead the table with class="basic-supp". Any idea how to fix it?
And the main problem is it doesn't work at all on complete html I posted above. The error:
print(tag.attrs['data-pricer'])
KeyError: 'data-pricer'
Can somebody give me advice please?
Thank you for your time!
P.S. This is not even close duplicate of post Extracting an attribute value with beautifulsoup