python - BeautifulSoup 字符串搜索
问题描述
我一直在谷歌搜索并查看其他关于在 BeautifulSoup 对象中搜索字符串的问题。
根据我的搜索,以下内容应该检测到字符串 - 但它没有:
strings = soup.find_all(string='Results of Operations and Financial Condition')
但是,以下检测字符串:
tags = soup.find_all('div',{'class':'info'})
for tag in tags:
if re.search('Results of Operations and Financial Condition',tag.text):
''' Do Something'''
为什么一个有效而另一个无效?
解决方案
您可能想使用:
strings = soup.find_all(string=lambda x: 'Results of Operations and Financial Condition' in x)
发生这种情况是因为实现find_all
查找您搜索的字符串以完全匹配。我想你可能有一些其他的文字旁边'Results of Operations and Financial Condition'
。
如果您在此处查看文档,您可以看到您可以为该string
参数提供一个函数,并且以下几行似乎是等效的:
soup.find_all(string='Results of Operations and Financial Condition')
soup.find_all(string=lambda x: x == 'Results of Operations and Financial Condition')
推荐阅读
- c# - 如何找到两个轴对齐立方体之间交集的体积和顶点?
- java - 使用 Java MethodChannel 在 Flutter 中运行 bytedeco tesseract 时应用程序崩溃
- go - 破坏对编码 pkg 的依赖。代码从一天到另一天都不起作用。升级失败?(编辑:不!失败的用户。请看我的回答)
- nginx - nginx + k8s:不更新环境变量
- javascript - 将子组件中单击的元素的值传递到父组件中的按钮中
- node.js - 如何将行分配给 node.js 上的变量?
- pytorch - 在 LSTM 中增加层数会增加 Pytorch 中的输入维度?
- java - 查找其中包含 if 语句的嵌套循环的大 O 表示法
- flutter - 在大滚动上,颤动中仅移动 1 个索引,就像颤动中的 instagram
- kubernetes - Kubernetes Autoscaling 使用 VPA - 关闭或自动更新模式?