-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.py
More file actions
148 lines (111 loc) · 3.98 KB
/
Crawler.py
File metadata and controls
148 lines (111 loc) · 3.98 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from collections import defaultdict
from nltk.stem.porter import *
from bs4 import BeautifulSoup
import spacy
import nltk
from nltk.corpus import brown
nltk.download('brown') # downloads the large nltk corpus of words
nlp = spacy.load("en_core_web_sm") # loads spacy's english core library
sw_spacy = nlp.Defaults.stop_words
with open('./url.txt','r') as f:
url_list = [line[:-1] for line in f]
# create list of all possible stemming for each root word -> possible = d[stemmer.stem('letting')]
vocab = set(brown.words())
stemmer = PorterStemmer()
d = defaultdict(set)
for v in vocab:
d[stemmer.stem(v)].add(v)
# check if given word is noun
def check_noun(word):
doc = nlp(word)
if(doc[0].tag_ == 'NNP'):
return True
else:
return False
# remove stopwords from text
def remove_stopwords(text):
res = []
for word in text:
if word not in sw_spacy:
res.append(word)
return res
# removes all special characters from string
def nospecial(text):
import re
text = re.sub("[^a-zA-Z0-9]+", "", text)
return text
# spider function to crawl the ted talks website for articles to get title and the keywords from the meta tag
start_url = url_list[-1]
# download and put the compatible chromedriver in this location
driver = webdriver.Chrome('./personalTests/chromedriver.exe')
def spider(url, titles, found):
driver.get(url)
timeout = 3
try:
# element_present = EC.presence_of_element_located(
# (By.ID, 'tabs--1--panel--0'))
element_present = EC.presence_of_element_located(
(By.ID, 'main'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print("Timed out waiting for page to load")
finally:
print("Page loaded")
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
title = soup.title.string.lower().split()
key = soup.select("meta[name='keywords']")[0]['content'].lower().split(",")
keywords = []
for k in key:
for word in k.split():
if word not in keywords:
keywords.append(word)
result = []
for i in range(0, len(title)):
title[i] = nospecial(title[i])
while("" in title):
title.remove("")
for t in ["ted", "talk"]:
title.remove(t)
for word in title:
if check_noun(word) and word not in result:
result.append(word)
for word in title:
if word in keywords and word not in result:
result.append(word)
for word in keywords:
try:
possible = d[stemmer.stem(word)]
possible = [p.lower() for p in possible]
for p in possible:
if p in title and p not in result:
result.append(p)
except:
pass
result = remove_stopwords(result)
og_title = " ".join(t for t in title)
if len(result) > 0 and og_title not in titles:
titles.append(og_title)
print(og_title)
print(result)
f = open('dataset2.txt', 'a')
f.write(og_title + "\t" + '_'.join(k for k in result) + "\n")
f.close()
f = open('url.txt', 'a')
f.write(url+"\n")
f.close()
divs = soup.find_all("a", {"class": "mb-5 block p-3"})
base_url = "https://www.ted.com"
for d in divs:
href = d.get_attribute_list("href")
if base_url+href[0] not in found:
found.append(base_url+href[0])
spider(base_url+href[0], titles, found)
spider(start_url, [], url_list)
driver.close()