如何使用 Beautifulsoup 删除锚标签的文本?

问题描述 投票:0回答:2

   <span class="companyName">
<a data-tn-element="companyName" class="turnstileLink companyOverviewLink" target="_blank" href="/cmp/Lush-Cosmetics" rel="noopener">LUSH Cosmetics</a>
</span>

我做了这个- item.find('span', class_ ='companyName').find('a').text

但是它给了我错误 - item.find('span', class_ ='companyName').find('a').text AttributeError:“NoneType”对象没有属性“text”

python html beautifulsoup
2个回答
0
投票

这应该可以解决问题

item.find('span',{'class':'companyName'}).find('a').text

工作代码

from bs4 import BeautifulSoup

content = '''
<div class="job_seen_beacon">
<span class="companyName">
<a data-tn-element="companyName" class="turnstileLink companyOverviewLink" target="_blank" href="/cmp/Lush-Cosmetics" rel="noopener">LUSH Cosmetics</a>
</span></div>
'''

soup = BeautifulSoup(content,features="lxml")
anchorText = soup.find('span',{'class':'companyName'}).find('a').text
print(anchorText)

0
投票

如果需要查找多个锚标签,同样的人会怎么想

© www.soinside.com 2019 - 2024. All rights reserved.