首页 > 解决方案 > Trim text from scraped element - Python / bs4

问题描述

I am using soup.find('div', class_='listing-agent').get_text() to get the result Listed by Joe Smith • ACME CO. I'm trying to just get Joe Smith. How can I trim up text to just get a name?

<div class="listing-agent">
  <span>
    "Listed by "
    <span>Joe Smith</span>
    <span>
    <span class="font-dot">•&lt;/span>
    <!-- -->
    "ACME CO"
    <!-- -->
  </span>
</div>

标签: python-3.xbeautifulsoup

解决方案


让:

a = soup.find('div', class_='listing-agent')

name = a.find_all('span')[0].find_all('span')[0].text # parsing the span

这给出了:

>>>name
Joe Smith

推荐阅读