使用BeautifulSoup提取数据

2024-09-28 19:33:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从文件中提取“7秒前结束”:

<div class="featured__columns">             
                            <div class="featured__column"><i style="color:rgb(149,213,230);" class="fa fa-clock-o"></i> <span title="Today, 11:49am">Ended 7 seconds ago</span></div>
                            <div class="featured__column featured__column--width-fill text-right"><span title="March 7, 2016, 10:50am">2 days ago</span> by <a style="color:rgb(149,213,230);" href="/user/Eclipsy">Eclipsy</a></div><a href="/user/Eclipsy" class="global__image-outer-wrap global__image-outer-wrap--avatar-small">
                                <div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/dc/dc5b8424bd5d17e13dcfe613689921dfc29f4574_medium.jpg);"></div>
                            </a>
                        </div>

我试着:

#!/usr/bin/python3
from bs4 import BeautifulSoup
with open("./source.html") as source_html:
    soup=BeautifulSoup(source_html.read())
    soup=soup.find_all("span")
    print(soup[0].string)

很好,但我觉得我的方法很愚蠢。提取数据有不同的方法吗?你知道吗


Tags: imagedivsourcestylehtmlcolumnrgbglobal
2条回答

您想要的跨度在第一个featured__columndiv

from bs4 import BeautifulSoup

html ="""<div class="featured__columns">
                            <div class="featured__column"><i style="color:rgb(149,213,230);" class="fa fa-clock-o"></i> <span title="Today, 11:49am">Ended 7 seconds ago</span></div>
                            <div class="featured__column featured__column width-fill text-right"><span title="March 7, 2016, 10:50am">2 days ago</span> by <a style="color:rgb(149,213,230);" href="/user/Eclipsy">Eclipsy</a></div><a href="/user/Eclipsy" class="global__image-outer-wrap global__image-outer-wrap avatar-small">
                                <div class="global__image-inner-wrap" style="background-image:url(https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/dc/dc5b8424bd5d17e13dcfe613689921dfc29f4574_medium.jpg);"></div>
                            </a>
                        </div>"""


print(BeautifulSoup(html).select("div.featured__column span")[0].text)
Ended 7 seconds ago

如果您特别想要第一个跨距或第n个跨距,您可以在选择中使用nth of type

In [53]: BeautifulSoup(html).select("div.featured__column span")
Out[53]: 
[<span title="Today, 11:49am">Ended 7 seconds ago</span>,
 <span title="March 7, 2016, 10:50am">2 days ago</span>]

In [54]: BeautifulSoup(html).select("div.featured__column span:nth-of-type(1)")
Out[54]: [<span title="Today, 11:49am">Ended 7 seconds ago</span>]

In [55]: BeautifulSoup(html).select("div.featured__column span:nth-of-type(2)")
Out[55]: [<span title="March 7, 2016, 10:50am">2 days ago</span>]
In [56]: BeautifulSoup(html).select("div.featured__column span:nth-of-type(2)")[0].text
Out[56]: u'2 days ago'

In [57]: BeautifulSoup(html).select("div.featured__column span:nth-of-type(1)")[0].text
Out[57]: u'Ended 7 seconds ago'

我们还可以将i标记与类fa fa-clock-o一起使用,并获得它的相邻同级跨度:

In [70]: BeautifulSoup(html).select("i.fa.fa-clock-o + span")
Out[70]: [<span title="Today, 11:49am">Ended 7 seconds ago</span>]

In [71]: BeautifulSoup(html).select("i.fa.fa-clock-o + span")[0].text
Out[71]: u'Ended 7 seconds ago'

最后,要准确地复制自己的逻辑,只得到第一个跨html,而不考虑类等。。您可以简化为:

BeautifulSoup(html).select("span:nth-of-type(1)")[0].text
BeautifulSoup(html).find("span").text

你可以试试

f_c = soup.find_all('div', class='featured__columns')[0]
print f_c.find('div', class='featured__column').span.get_text()

类似地,如果有多个带有类divfeatured__columns标记,那么您可以遍历它并获取数据。你知道吗

相关问题 更多 >