-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.py
More file actions
65 lines (58 loc) · 2.35 KB
/
Page.py
File metadata and controls
65 lines (58 loc) · 2.35 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
'''
Nicholas Lin
Page.py
4/2/20
'''
class Page:
headers = []
headline = ""
articles = []
page_type = ""
def __init__(self, soup= "", page_type = "none"):
if(soup != ""):
self.page_type = page_type
self.soup = soup
self.init_articles(soup)
self.init_headers(soup)
if(page_type == "Home"):
self.init_headline(soup)
# Stores headers in dictionary of the form [header_text]
def init_headers(self, soup):
self.headers = []
if(self.page_type == "Home"):
headers_html = soup.find('div', {"data-testid" : "masthead-mini-nav"})
elif(soup.find('nav', {"class" : "e1se7h4u0"}) != None):
headers_html = soup.find('nav', {"class" : "e1se7h4u0"})
else:
return
for header in headers_html.findAll('a'):
text = header.text
# link = header['href']
#self.headers[text] = link.split(".com")[1]
self.headers.append(text)
# Returns headline = {headline_text : headline_url_postfix}
def init_headline(self, soup):
if(soup.find('div', {'class':'css-1t1jowt'}) != None):
headline_html = soup.find('div', {'class':'css-1t1jowt'})
headline_text = headline_html.find('span').text
self.headline = headline_text
else:
self.headline = ""
#headline_link = soup.find('div', {"class":"css-1t1jowt"}).a["href"]
#self.headline = {headline_text : headline_link}
#TODO: Make sure all articles are actually articles
# Returns list of articles in the form [article1_title, article2_title, ...]
def init_articles(self, soup):
self.articles = []
if soup.find('section', {"data-testid":"block-Briefings"}):
soup.find('section', {"data-testid":"block-Briefings"}).decompose()
for article in soup.findAll('article'):
article_html = article.find('h2')
if(article_html != None):
if(article_html.find('span') != None):
article_title = article_html.find('span').text
else:
article_title = article_html.text
#article_link = article_html.a["href"]
#article = {article_title : article_link}
self.articles.append(article_title)