import requests
from bs4 import BeautifulSoup
import operator
# Create a list of words
def start(url):
word_list = []
source_code = requests.get(url).text
soup = BeautifulSoup(source_code)
# loop through each post and get text
for post_text in soup.findAll('a', {'class': 'index_singleListingTitles'}):
content = post_text.string
# break each post up into a list of words
words = content.lower().split()
for each_word in words:
word_list.append(each_word)
clean_up_list(word_list)
# Lowercase and remove odd symbols
def clean_up_list(word_list):
clean_word_list = []
for word in word_list:
symbols = "!@#$%^&*()_+{}|:<>?,./;'[]\=-\""
for i in range(0, len(symbols)):
word = word.replace(symbols, "")
if len(word) > 0:
clean_word_list.append(word)
create_dictionary(clean_word_list)
# Create dictionary with word counts
def create_dictionary(clean_word_list):
word_count = {}
for word in clean_word_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# sort this dictionary by (0 for key, 1 for values)
for key, value in sorted(word_count.items(), key=operator.itemgetter(1)):
print(key, value)
start('https://buckysroom.org/tops.php?type=text&period=this-month')
Python 35-37 Programming Tutorial - Word Frequency Counter
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...