BeautifulSoup获得一定价值

时间:2018-08-27 13:28:10

标签: python beautifulsoup

我正在使用一个非常简单的方法来获取所需的信息:

a = soup.find_all(class_ = "pull-right hidden-phone")
print(a)
print(a[0])

输出为:

[<span class="pull-right hidden-phone"><span data-c="190000000" data-time="1535345254000">1.9 BTC</span></span>, <span class="pull-right hidden-phone"><span data-c="4890548" data-time="1535345254000">0.04890548 BTC</span></span>]
<span class="pull-right hidden-phone"><span data-c="190000000" data-time="1535345254000">1.9 BTC</span></span>

我想获得1.9 BTC或190000000,但不知道如何获得。 我已经尝试过print(a[0]["data-time"]),但它不起作用,它说

  

返回self.attrs [key] KeyError:“数据时间”

但是,此print(a[0]["class"])可以工作并给出['pull-right', 'hidden-phone']

那么我如何获得1.9 BTC或190000000?

2 个答案:

答案 0 :(得分:0)

使用print(a.find("span").span["data-time"])print(a.span.span["data-time"])

例如:

from bs4 import BeautifulSoup
s = """<span class="pull-right hidden-phone"><span data-c="190000000" data-time="1535345254000">1.9 BTC</span></span>"""
a = BeautifulSoup(s, "html.parser")

print(a.find("span").span["data-time"])
print(a.span.span["data-c"])
print(a.span.span.text)

输出:

1535345254000
190000000
1.9 BTC

答案 1 :(得分:0)

您应该尝试这种方式,希望有帮助

from bs4 import BeautifulSoup
text = """
<span class="pull-right hidden-phone"><span data-c="190000000" data-time="1535345254000">1.9 BTC</span></span>, 
<span class="pull-right hidden-phone"><span data-c="4890548" data-time="1535345254000">0.04890548 BTC</span></span>
"""

soup = BeautifulSoup(text, 'html.parser')
for tag in soup.find_all('span', attrs={'class': 'pull-right hidden-phone'}):
    span_tag = tag.span
    print('Attribute Value:', span_tag.get('data-c'), 'Date-time:', span_tag.get('data-time'), 'Tag Text:', span_tag.get_text())


# Output as: 
# Attribute Value: 190000000 Date-time: 1535345254000 Tag Text: 1.9 BTC
# Attribute Value: 4890548 Date-time: 1535345254000 Tag Text: 0.04890548 BTC