简体   繁体   English

如何通过python中的漂亮汤在html页面中找到特定单词?

[英]How to find a particular word in html page through beautiful soup in python?

I want to find how many times a particular word has come in a web page through beautiful soup within that html text ? 我想通过该html文本中的漂亮内容来查找某个单词在网页中出现了多少次? I tried out the findAll function but finds only words within a particular tag like soup.body.findAll will find the particular word within the body tag but I want it to search that word within all tags that in in the html text. 我尝试了findAll函数,但仅在特定标签(例如soup.body.findAll找到单词soup.body.findAll将在body标签中找到特定单词,但我希望它在html文本中的所有标签中搜索该单词。 Also once I find that word I need to create a list of word just coming before and after that word, can someone please help me how to do so ? 同样,一旦找到该单词,我需要创建该单词前后的单词列表,有人可以帮我怎么做吗? Thanks. 谢谢。

According to the newest BeautifulSoup 4 api you can use recursive keyword to find the text in the whole tree. 根据最新的BeautifulSoup 4 API,您可以使用recursive关键字在整个树中查找文本。 You will have strings that then you can operator on and seperate the words. 您将拥有字符串,然后您可以对其进行运算并分隔单词。

Here is a complete example: 这是一个完整的示例:

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)

it outputs: 它输出:

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 另请参阅此处的字符串关键字参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM