如何从相同的标签中找到某个 <> 标签并从中提取文本? BeautifulSoup

How to locate a certain <>tag from the same tags and extract the text from it? BeautifulSoup

我正在使用 BeautifulSoup 从中提取职位名称,确实有 s span <> 标签,其中一个包含职位信息。

h2标签:

<h2 class="jobTitle jobTitle-newJob">
  <div class="new topLeft holisticNewBlue desktop">
      <span class="label">new</span>
  </div>
      <span title="Entry Level Software Developer">Entry Level Software Developer</span>
</h2>

这是我的一段代码示例:

divs = soup.find_all("div", class_="job_seen_beacon")
for item in divs:
    title_span = item.find('h2', class_="jobTitle")
    title = title_span.find_all(title=True)

当运行时,我只能获取包含标题的列表。

[<span title="Entry Level Software Developer">Entry Level Software Developer</span>]

如何从中提取标题文本,或者有其他方法可以执行此任务吗?

您需要获取它的文本 属性。

title = [span.text for span in title_span.find_all(title=True)]

我的猜测,只有一个标题,你可以使用:

title = title_span.find(title=True)