[英]Get first text inside a tag using beautiful soup
我需要从跨度标签中获取工资范围。 我尝试使用 find().text 但由于 span 标签中有其他标签,所以它不起作用。
job_list = soup.find_all("div", class_="d-flex flex-column pl-sm css-1buaf54 job-search-key- 1mn3dn8 e1rrn5ka0")
for job in job_list:
salary = job.find("span", class_="job-search-key-1hbqxax e1wijj240")
print(salary)
我的输出:
₹362,870 - ₹955,252 是我想要的输出
您在这里真正想要做的是获取 div 类中的文本,而不是其中的嵌套标签中的文本。
您可以将soup.find
与选项text = True and recursive = False
一起使用
from bs4 import BeautifulSoup
html_doc = '''<span class="job-search-key-1hbqxax elwijj240" data-test="detailsalary">
₹362,870 - ₹955,252 <span class="job-search-key- elwijj242">(Glassdoor Est.)</span> <span class="SVGInline greyInfoIcon" data-test="salaryIcon"> <svg class="SVGInline-svg greyInforcon-svg" height="16" viewbox="0 @ 16 16" width="16" xmlns="http://www.w3.org/2000/svg"> <g fill="none" fill-rule="evenodd" id="prefix__info-16-px" stroke="none" stroke-width="1">
<path d="M8 14A6 6 @ 118 2a6 6 @ 010 12zme- 1A5 5 @ 108 3a5 5 @ 880 18zm-.6-5.60.6.6 8 111.2 8v11a.6.6 @ 01-1.2 8v7.42MS 5.62.6.6 110-1.2.6.6 @ 810 1.2z" fill="#505863" id="prefix_a"></path>
</svg> </span>
<div class="d-none"></div> </span>'''
soup = BeautifulSoup(html_doc, 'html.parser')
soup.find(class_='job-search-key-1hbqxax elwijj240').find(text=True, recursive=False).strip()
这给了我们
'₹362,870 - ₹955,252'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.