[英]Retrieving data using Beautiful Soup
So I've been trying to retrieve some data using BeautifulSoup but I've hit a brick wall. 因此,我一直在尝试使用BeautifulSoup检索一些数据,但遇到了麻烦。
<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>
</tr>
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. 我希望脚本执行的操作是接受输入(在这种情况下为“类似于板岩的颜色”字符串),并使其返回下面的数据(0 / 10、14 ref等),以便我可以对其进行比较到另一组数据。 How can I make it work? 我该如何运作?
similar_color = soup.find('tr', {'data-name': 'A Color Similar to Slate'})
for value in similar_color.find_all('td'):
print(value.text)
Should result in: 应导致:
0/10
35,000
and so on, so forth. 依此类推。 However, it seems like you want to grab the text value sometimes, and the data-conversion
value other times. 但是,似乎您想有时获取文本值,而有时则要获取data-conversion
值。 To do that, you would just substitute the print(value.text)
line with: 为此,您只需将print(value.text)
行替换为:
print(value.attrs.get('data-conversion'))
In case you will use it on other HTML style files: 如果您将其用于其他HTML样式文件:
from bs4 import BeautifulSoup
html= """<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>
</tr>"""
soup = BeautifulSoup(html)
texts = [i.get_text() for i in soup.find_all() if i.get_text()]
print(texts[texts.index('A Color Similar to Slate'):])
This checks all the tags not just td
. 这将检查所有标签,而不仅仅是td
。 The output is ['A Color Similar to Slate', 'A Color Similar to Slate', 'A Color Similar to Slate', '0/10', '35,000', '32,550', '33,775']
输出为['A Color Similar to Slate', 'A Color Similar to Slate', 'A Color Similar to Slate', '0/10', '35,000', '32,550', '33,775']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.