我使用 BeautifulSoup 和 Python 2.7 从 IMDB 的移动站点中删除了以下代码。
我想为剧集编号“1”、标题“冬天来了”和 IMDB 评分“8.9”创建一个单独的对象。似乎无法弄清楚如何拆分剧集编号和标题。
<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
<span class="text-large">
1.
<strong>
Winter Is Coming
</strong>
</span>
<br/>
<span class="mobile-sprite tiny-star">
</span>
<strong>
8.9
</strong>
17 Apr. 2011
</a>
请您参考如下方法:
您可以使用find 将类text-large 的span 定位到您需要的特定元素。
一旦你有了你想要的 span,你可以使用 next 来抓取下一行,包含剧集号和 find 来定位strong 包含标题
html = """
<a class="btn-full" href="/title/tt1480055?ref_=m_ttep_ep_ep1">
<span class="text-large">
1.
<strong>
Winter Is Coming
</strong>
</span>
<br/>
<span class="mobile-sprite tiny-star">
</span>
<strong>
8.9
</strong>
17 Apr. 2011
</a>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
span = soup.find('span', attrs={'text-large'})
ep = str(span.next).strip()
title = str(span.find('strong').text).strip()
print ep
print title
> 1.
> Winter Is Coming
