我有以下html:
from bs4 import BeautifulSoup as soup
html_doc = """<div class="__cast-member" content="Vishal Krishna Reddy" itemprop="name"><div class="__cast-image wow fadeIn"><meta content="https://in.bmscdn.com/iedb/artist/images/website/poster/large/vishal-krishna-reddy-16275-24-03-2017-15-17-54.jpg" itemprop="image"><img alt="Vishal Krishna Reddy" data-error="//in.bmscdn.com/webin/profile/user.jpg" data-lazy="//in.bmscdn.com/iedb/artist/images/website/poster/large/vishal-krishna-reddy-16275-24-03-2017-15-17-54.jpg" title="Vishal Krishna Reddy"/></meta></div><br/>Developer<br><span class="__role">Actor</span><br><span class="__characterName">As Kathiravan</span></br></br></div>"""
html = soup(html_doc, "html.parser")
Cast=html.find("div", {"class":"__cast-member"})
print Cast.text
输出:DeveloperActorAs Kathiravan
但我只需要输出:开发人员
答案 0 :(得分:1)
您可以使用.next_sibling
属性来获取所需的文本。首先找到<div>
标记class="__cast-image wow fadeIn"
。您想要的文本位于此标记之后。因此,请在此代码上使用.next_sibling
。但首先你会得到<br/>
,所以再次使用它。
>>> soup.find('div', class_='__cast-image').next_sibling
<br/>
>>> soup.find('div', class_='__cast-image').next_sibling.next_sibling
'Developer'