python - BeautifulSoup 查找所有出现的特定文本

标签 python html parsing beautifulsoup

我将分析许多具有不同 html 的网站,并尝试使用 BeautifulSoup 查找包含特定文本(在 html 内)的所有行。

r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")               
for text in soup.find_all():
    if "price" in text:
        print text

这种方法不起作用(尽管“价格”在 html 中被提及超过 40 次)。也许有更好的方法来做到这一点?

最佳答案

为什么不让 BeautifulSoup 找到包含所需文本的节点:

for node in soup.find_all(text=lambda x: x and "price" in x):
    print(node)

关于python - BeautifulSoup 查找所有出现的特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176278/

相关文章:

python - 为什么 discord.py 无法识别 "on_message()"之外的变量?

javascript - 为什么我的文本在移动设备 View 上没有响应?

html - 文本溢出 :ellipsis and Flexbox

html - 使 flexbox 行成为最短子元素的高度?

c++ - 解析文件中的数据并存储在对象中

python - 你如何使用 BeautifulSoup 从 HTML 'datacell' 中获取文本

python - Plumbum - 嵌套 SSH 远程处理/跳转主机

python - BeautifulSoup 成功写入 html 但 find_all 什么也没返回

javascript - 如何在网站上使用 BeautifulSoup 或 Slimit 从 javascript 变量输出电子邮件地址

python - 在 python 中拆分标签