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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卡辰,“玉大人胞皱,你說我怎么就攤上這事【怕瑁” “怎么了反砌?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長允蚣。 經(jīng)常有香客問我于颖,道長,這世上最難降的妖魔是什么嚷兔? 我笑而不...
- 正文 為了忘掉前任森渐,我火速辦了婚禮,結(jié)果婚禮上冒晰,老公的妹妹穿的比我還像新娘同衣。我一直安慰自己,他們只是感情好壶运,可當(dāng)我...
- 文/花漫 我一把揭開白布耐齐。 她就那樣靜靜地躺著,像睡著了一般蒋情。 火紅的嫁衣襯著肌膚如雪埠况。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼慎恒,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了赎瞎?” 一聲冷哼從身側(cè)響起牌里,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎务甥,沒想到半個(gè)月后二庵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贪染,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年催享,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了杭隙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站以故,受9級(jí)特大地震影響蜗细,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜怒详,卻給世界環(huán)境...
- 文/蒙蒙 一炉媒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昆烁,春花似錦吊骤、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鼠渺,卻和暖如春鸭巴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拦盹。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像池磁,于是被迫代替她去往敵國和親奔害。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...