【问题标题】:How to find a particular word in html page through beautiful soup in python?如何通过python中的美汤找到html页面中的特定单词?
【发布时间】:2016-01-28 13:33:31
【问题描述】:

我想通过该 html 文本中的漂亮汤来查找特定单词在网页中出现了多少次? 我尝试了findAll 函数,但只找到特定标记中的单词,如soup.body.findAll 会在正文标记中找到特定单词,但我希望它在 html 文本中的所有标记中搜索该单词。 此外,一旦我找到那个词,我需要创建一个在那个词之前和之后的词列表,有人可以帮我怎么做吗?谢谢。

【问题讨论】:

标签: python python-2.7 beautifulsoup


【解决方案1】:

根据newest BeautifulSoup 4 api,您可以使用recursive 关键字在整个树中查找文本。您将拥有字符串,然后您可以对其进行操作并分隔单词。

这是一个完整的例子:

import bs4
import re

data = '''
<html>
<body>
<div>today is a sunny day</div>
<div>I love when it's sunny outside</div>
Call me sunny
<div>sunny is a cool word sunny</div>
</body>
</html>
'''

searched_word = 'sunny'

soup = bs4.BeautifulSoup(data, 'html.parser')
results = soup.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)

print 'Found the word "{0}" {1} times\n'.format(searched_word, len(results))

for content in results:
    words = content.split()
    for index, word in enumerate(words):
        # If the content contains the search word twice or more this will fire for each occurence
        if word == searched_word:
            print 'Whole content: "{0}"'.format(content)
            before = None
            after = None
            # Check if it's a first word
            if index != 0:
                before = words[index-1]
            # Check if it's a last word
            if index != len(words)-1:
                after = words[index+1]
            print '\tWord before: "{0}", word after: "{1}"'.format(before, after)

它输出:

Found the word "sunny" 4 times

Whole content: "today is a sunny day"
    Word before: "a", word after: "day"
Whole content: "I love when it's sunny outside"
    Word before: "it's", word after: "outside"
Whole content: "
Call me sunny
"
    Word before: "me", word after: "None"
Whole content: "sunny is a cool word sunny"
    Word before: "None", word after: "is"
Whole content: "sunny is a cool word sunny"
    Word before: "word", word after: "None"

Also see here's string keyword reference

【讨论】:

  • results = soup.body.find_all(string=searched_word, recursive=true) NameError: name 'true' is not defined
  • 我用完整的工作示例更新了答案,请再次检查
  • 我正在使用 python 2.7.3 得到“找到“阳光”这个词 0 次”?我只是复制粘贴你的示例代码
  • 似乎string关键字是在4.4版本中添加的,所以使用它或将soup.body.find_all(string=...)更改为soup.body.find_all(text=...)(4.3及之前的不同关键字)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2019-05-15
  • 1970-01-01
相关资源
最近更新 更多