-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmdScrape.py
60 lines (49 loc) · 1.73 KB
/
tmdScrape.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
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
#!/usr/bin/python
from networkScrape import NetworkScraper
class TMDNetwork(NetworkScraper):
"""docstring for TMDNetwork"""
def __init__(self):
super(TMDNetwork, self).__init__()
def getDataSource(self, nodeId):
# print "Entering getDataSource"
print nodeId
PROF_ADDR = lambda articleName: "https://www.michigandaily.com/section/" + articleName
soup = self.url_to_soup(PROF_ADDR(nodeId))
return soup
def getEdgeData(self, data):
nodebox = data.find_all("div", class_ = "mlt")[0]
links = [x['href'] for x in nodebox.find_all("a")]
article_IDs = [x.replace("/section/","") for x in links if "/section/" in x]
edgeObjs = [self.makeEdgeObject(a_id) for a_id in article_IDs]
return edgeObjs
def getNodeName(self, data):
titleDiv = data.find_all("div", class_ = "pane-node-title")[0]
# profileData = data.find_all("div", class_ = "header-box")[0]
# name = profileData.find("p", class_ = "articleName").text
# return name
return titleDiv.text.strip()
def getNodeProperties(self, data):
propertiesObj = {}
kicker = data.find("div", class_ = "pane-node-field-kicker")
if kicker:
kicker = kicker.text.strip()
else:
kicker = ""
url = data.find("link", rel = "canonical")['href']
urlPath = url.replace("https://www.michigandaily.com/","").split('/')
contentType = urlPath[0]
if len(urlPath) > 1:
section = urlPath[1]
else:
section = ""
if len(urlPath) > 2:
linkTitle = urlPath[2]
else:
linkTitle = ""
shortId = int(data.find("link", rel = "shortlink")['href'].split("/")[-1])
propertiesObj['kicker'] = kicker
propertiesObj['contentType'] = contentType
propertiesObj['section'] = section
propertiesObj['linkTitle'] = linkTitle
propertiesObj['shortId'] = shortId
return propertiesObj