-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
167 lines (117 loc) · 5.54 KB
/
file.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# download all files retrieved from acs published on Journal of Medicinal Chemistry wih the given keyword
import requests
from html.parser import HTMLParser
import sys
addressArr = []
DOMAIN = "https://pubs.acs.org"
class QueryParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.articleTagFound = False
self.pageListFound = False
self.nextButtonFound = False
def handle_starttag(self, tag, attrs):
if (tag == "h2" and len(attrs) == 1 and attrs[0][1] == "issue-item_title"):
self.articleTagFound = True
elif (self.articleTagFound and tag == "a"):
global addressArr, DOMAIN
addressArr.append(DOMAIN + attrs[0][1])
self.articleTagFound = False
elif (tag == "ul" and len(attrs) == 1 and attrs[0][1] == "rlist--inline pagination__list"):
self.pageListFound = True
elif (self.pageListFound and tag == "span"):
self.nextButtonFound = True
elif (self.nextButtonFound and tag == "a"):
self.pageListFound = False
self.nextButtonFound = False
for attr in attrs:
if (attr[0] == "href"):
QueryParserHandler.nextPageURL = attr[1]
def handle_endtag(self, tag):
if(self.pageListFound and tag == "nav"):
self.pageListFound = False
self.nextButtonFound = False
QueryParserHandler.hasNextPage = False
class QueryParserHandler:
hasNextPage = True
nextPageURL = ""
keyWords = []
for i in range(1, len(sys.argv)):
keyWords.append(sys.argv[i])
URLs = []
for keyWord in keyWords:
queryString = ""
for word in keyWord.split():
if(not queryString):
queryString += word
else:
queryString += f"+{word}"
URLs.append((keyWord, f"https://pubs.acs.org/action/doSearch?field1=AllField&text1={queryString}&field2=AllField&text2=&ConceptID=&ConceptID=&publication=&publication%5B%5D=jmcmar&accessType=allContent&Earliest="))
keyWordId = 0
for URL in URLs:
addressArr.clear()
QueryParserHandler.hasNextPage = True
response = requests.get(URL[1])
queryParser = QueryParser()
queryParser.feed(response.text)
while(QueryParserHandler.hasNextPage):
response = requests.get(QueryParserHandler.nextPageURL, headers = {"User-Agent": "Mozilla/5.0"})
queryParser.feed(response.text)
fileId = 0
for address in addressArr:
articleResponse = requests.get(address)
with open(f"files/{keyWords[keyWordId]}/file{fileId}.html", "w", encoding="utf-8") as outputFile:
class TableParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.HTML = ""
self.ICFound = False
self.keywordFound = False
self.abstractFound = False
self.paragraphFound = False
self.paragraphDivCount = 0
def handle_starttag(self, tag, attrs):
self.HTML += (self.get_starttag_text())
if(self.keywordFound):
return
if(tag == "p" and len(attrs) == 1 and attrs[0][1] == "articleBody_abstractText"):
self.abstractFound = True
if(tag == "div" and len(attrs) == 1 and "NLM_p" in attrs[0][1]):
self.paragraphFound = True
self.paragraphDivCount += 1
elif(self.paragraphFound and tag == "div"):
self.paragraphDivCount += 1
def handle_data(self, data):
self.HTML += (data)
if(self.keywordFound):
return
if(self.paragraphFound or self.abstractFound):
stringList = ["ic50", "ec50", "ki50", "kd50", "ed50"]
if(any(substring in data.lower().strip() for substring in stringList)):
self.keywordFound = True
elif(self.ICFound):
if(len(data) >= 2 and data[:2] == "50"):
self.keywordFound = True
else:
self.ICFound = False
elif(len(data) >= 2 and (data.lower().strip()[-2:] in ["ic", "ec", "ki", "kd", "ed"])):
self.ICFound = True
def handle_endtag(self, tag):
self.HTML += (f"</{tag}>")
if(self.keywordFound):
return
if(self.abstractFound and tag == "p"):
self.abstractFound = False
if(self.paragraphFound and tag == "div" and self.paragraphDivCount == 1):
self.paragraphFound = False
self.paragraphDivCount -= 1
elif(self.paragraphFound and tag == "div" and self.paragraphDivCount > 1):
self.paragraphDivCount -= 1
tableParser = TableParser()
tableParser.feed(articleResponse.text)
if(tableParser.keywordFound):
outputFile.write(tableParser.HTML)
else:
print(address)
fileId += 1
keyWordId += 1