python - Python BeautifulSoup - find_all 捕获由空格和新行包围的字符串
问题描述
我有以下 HTML 片段,它们是更大的 HTML 页面的一部分:
<tr >
<th class="left">
<span tooltip haspopup="true" class="tip" title="A type of fruit">Oranges</span>:
</th>
<td class="reduce">
Seven
</td>
</tr>
<tr >
<th class="left">
Apples
</th>
<td>
Three
</td>
</tr>
当我执行代码时:
soup.find_all(string='Oranges')
我得到:
['Oranges']
这是完美的。
但是,当我执行代码时:
soup.find_all(string='Apples')
我得到:
[]
为什么这不起作用?我感觉这与 HTML 代码的“Apples”位周围的空格和换行符等有关,但是我无法抓住它。我已经尝试了以下没有结果的方法。
soup.find_all(string='\n Apples\n ')
soup.find_all(string=' Apples ')
soup.find_all(string=' Apples ')
感谢您的帮助!谢谢。
Ps 我不认为这很重要,但最终我使用“findParent().fetchNextSiblings()[0].text.strip()”或类似的方法来获得“七”和“三” - 它适用于前一种情况,但不是后一种情况。
解决方案
尝试:
import re
...
soup.find_all(text = re.compile(r"Apples", re.IGNORECASE))
推荐阅读
- java - 如何使用 JSOUP 提取 css 样式
- reactjs - 类型“EventTarget & HTMLSelectElement”上不存在属性“值”
- java - 当从数据库中获取数据时,只获取一行的第一个索引,我想要每一行的所有数据
- c - 是否可以在函数中获取由返回值初始化的变量的内存地址?
- python - 无法完成操作:Elasticsearch AWS 中的 create_index
- c# - 通过正则表达式将括号中的字典键替换为值
- c# - 是否有通用解决方案来防止/避免在 web api C# 中过度发布?
- sharepoint - Sharepoint GetFileByServerRelativeUrl 返回文件不存在
- openid - 未找到 node-oidc-provider 授权码
- powerbi - 在这种情况下使用“计算”与不使用有区别吗?