-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_frequency_counter.py
35 lines (31 loc) · 1.01 KB
/
word_frequency_counter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests
from bs4 import BeautifulSoup
import operator
def start(url):
word_list=[]
source_code=requests.get(url).text
soup=BeautifulSoup(source_code)
for post_text in soup.findAll('a',{'class':'index_singleListingTitles'}):
content=post_text.string
words=content.lower().split()
for each_word in words:
word_list.append(each_word)
clean_up_list(word_list)
def clean_up_list(word_list):
clean_list=[]
for word in word_list:
symbol='!@#$%^&*()\"\':'
for i in range(0,len(symbol)):
word =word.replace(symbol[i]," ")
if len(word) > 0:
clean_list.appen(word)
create_dictionary(clean_list)
def create_dictionary(clean_list):
word_count={}
for word in clean_list:
if word in word_count:
word_count[word] +=1
else:
word_count[word] =1
for key,value in sorted(word_count.items(),key=operator.itemgetter(1)):
print(key,value)