-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcrawler.py
More file actions
71 lines (52 loc) · 1.79 KB
/
Copy pathwebcrawler.py
File metadata and controls
71 lines (52 loc) · 1.79 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
from bs4 import BeautifulSoup
import operator
from collections import Counter
'''Function defining the web-crawler/core
spider, which will fetch information from
a given website, and push the contents to
the second function clean_wordlist()'''
def start(url):
# empty list to store the contents of the website fetched from our web-crawler
wordlist = []
source_code = requests.get(url).text
# BeautifulSoup object which will ping the requested url for data
soup = BeautifulSoup(source_code, 'html.parser')
# Text in given web-page is stored under
# the <div> tags with class <entry-content>
for each_text in soup.find_all('div', {'class': 'entry-content'}):
content = each_text.text
# use split() to break the sentence into words and convert them into lowercase
words = content.lower().split()
for each_word in words:
wordlist.append(each_word)
clean_wordlist(wordlist)
# Function removes any unwanted symbols
def clean_wordlist(wordlist):
clean_list = []
for word in wordlist:
symbols = '!@#$%^&*()_-+={[}]|\;:"<>?/., '
for i in range(0, len(symbols)):
word = word.replace(symbols[i], '')
if len(word) > 0:
clean_list.append(word)
create_dictionary(clean_list)
# Creates a dictionary containing each word's count and top_20 occurring words
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("% s : % s " % (key, value))
c = Counter(word_count)
# returns the most occurring elements
top = c.most_common(10)
print(top)
start("https://www.geeksforgeeks.org/programming-language-choose/")
print('I am in master')
print('Hi there')
print('this is branch1')
print("hello")