Disco Hamster Disco Hamster -4 years ago 125
Python Question

Retrieving data using Beautiful Soup

So I've been trying to retrieve some data using BeautifulSoup but I've hit a brick wall.

<tr data-name="A Color Similar to Slate">
<th class="unique"><a href="/item/5052/6/223d382afee2ac6857d3298b800652e0" class="item-link"><span style='color: #7D6D00'>A Color Similar to Slate</span></a></th>
<td class=unique>0/10</td>
<td class="unique" data-conversion="14 ref">35,000</td>
<td class="unique" data-conversion="13.02 ref">32,550</td>
<td class="unique" data-conversion="13.51 ref">33,775</td>
<td class="unique" style="text-align: center;"><a class="item-link-backpack" href="http://backpack.tf/stats/Unique/A+Color+Similar+to+Slate/tradable/craftable"><img src="/img/bptf-icon.png" alt="View on Backpack.tf"/></a></td>

What I'd like my script to do is to take an input (in this case a "A Color Similar to Slate" string) and have it return the data below(0/10, 14 ref etc) so that I can compare it to a different set of data. How can I make it work?

Answer Source
similar_color = soup.find('tr', {'data-name': 'A Color Similar to Slate'})
for value in similar_color.find_all('td'):

Should result in:


and so on, so forth. However, it seems like you want to grab the text value sometimes, and the data-conversion value other times. To do that, you would just substitute the print(value.text) line with:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download