python-3.x - 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">•</span>
<!-- -->
"ACME CO"
<!-- -->
</span>
</div>
解决方案
让:
a = soup.find('div', class_='listing-agent')
name = a.find_all('span')[0].find_all('span')[0].text # parsing the span
这给出了:
>>>name
Joe Smith
推荐阅读
- python - Convert Sql server raw query through SqlAlchemy Orm in python
- python - 在Python中的数据框中成对的行之间减去
- excel - @符号放在间接前面时有什么作用?
- ruby-on-rails - 从 sidekiq 访问不同 docker 容器中的活动存储对象
- css - 我希望第一张图片上有文字,但第二张没有 - 可滑动
- javascript - TypeScript react - JSX 元素类型“Modal.Header”没有任何构造或调用签名
- python - 如何从神经网络模型输出中解码标准化值?
- powershell - 在 Powershell 5 中更改 PSModulePath
- javascript - 如何在屏幕中间生成不重叠的随机粒子?
- gitlab - 如何通过 API 比较 GitLab 中的两个不同标签?