Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python find out the number of times a keyword appears?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how python finds out the number of times a keyword appears. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Topic: how to find out the number of times a keyword appears in the corresponding URL page, for example, how many times "python" appears in the https://www.ershicimi.com/" page?

First of all, we decompose the problem, and the first step is to find a way to get the content of the web page, which is very simple, which can be done with the requests library. The second step is to clean the data, remove html tags to extract text content, you can use BeautifuSoup, you can also use Requests-html to solve, Python method is good, more wheels. The third step is to count according to the keywords that appear in the text.

Ignore the first two steps, and let's focus on the third step.

There are three ways to get the number of occurrences of keywords, depending on your own requirements. One is to use the API method string.count () provided by the string, which is easiest to return the number of occurrences directly. For example:

I follow you, you follow me. Count ("follow")

two

However, this approach has a limitation, if you want to know where the keyword appears, this can not be achieved, you need to use the second method, which is the regular expression, regular can be said to be one of the most powerful tools for dealing with strings. No character operations are involved, so don't ignore the regularities.

> for m in re.finditer ("(follow)", "I follow you, you follow me"):

... Print (m.start ())

...

one

seven

Use the start () method of the match object to know where the matching character appears. This example tells us that keywords appear in the first and seventh positions.

If this is not enough, you want to know the words that appear most frequently, if it is easy to say in English, separate the words according to the spaces and count them one by one, and use the collections.Counter module to achieve, but not Chinese, there are no spaces between Chinese words, so you need to use Chinese word segmentation tools to segment sentences. Jieba participle is commonly used.

Import jieba

From collections import Counter

Def get_words (txt):

Seg_list = jieba.cut (txt)

C = Counter ()

For x in seg_list:

If len (x) > 1 and x! ='\ r\ n':

C [x] + = 1

Print ('word frequency statistics')

For (kpaper v) in c.most_common (100):

Print (f'{k}: {v}') above is all the content of the article "how does python find out the number of times a keyword appears?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report