我需要从文件中提取“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)
很好,但我觉得我的方法很愚蠢。提取数据有不同的方法吗?你知道吗
您想要的跨度在第一个
featured__column
div
:如果您特别想要第一个跨距或第n个跨距,您可以在选择中使用nth of type:
我们还可以将
i
标记与类fa fa-clock-o
一起使用,并获得它的相邻同级跨度:最后,要准确地复制自己的逻辑,只得到第一个跨html,而不考虑类等。。您可以简化为:
你可以试试
类似地,如果有多个带有类
div
的featured__columns
标记,那么您可以遍历它并获取数据。你知道吗相关问题 更多 >
编程相关推荐