-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglescholarspider.py
213 lines (165 loc) · 6.53 KB
/
googlescholarspider.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding:utf-8 -*-
import re
import time
import copy
import requests
from bs4 import BeautifulSoup
from threads.spiderthread import ReferenceSpiderThread
from dbspider.acmspider import ACMSpider
from dbspider.ieeespider import IEEESpider
from dbspider.springerspider import SpringerSpider
from dbspider.sciencedirectspider import ScienceDirectSpider
class SpiderConfig(object):
def __init__(self):
self.config = {}
self.config['keyword'] = ''
self.config['depth'] = 0
self.config['breadth'] = 0
self.config['googlescholar_base'] = ''
self.config['headers'] = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Host': 'c3.glgooo.top',
'Referer': 'https://c3.glgooo.top/scholar/',
'Upgrade-Insecure-Requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
def set_config(self, key, val):
if key and val and key in self.config.keys():
self.config[key] = val
class GoogleScholarSpider(object):
def __init__(self, spiderconfig):
self.spiderconfig = spiderconfig.config
self.nextround_urls = {}
self.nextround_urls['ScienceDirect'] = []
self.nextround_urls['IEEE'] = []
self.nextround_urls['Springer'] =[]
self.nextround_urls['ACM'] = []
def duplicateThisRoundUrls(self, thisround_urls):
duplicate = lambda x,y:x if y in x else x + [y]
for db in ['ScienceDirect','IEEE','Springer','ACM']:
urls = thisround_urls[db]
urls = reduce(duplicate, [[], ] + urls)
thisround_urls[db] = urls
return thisround_urls
def resetNextRoundUrls(self):
self.nextround_urls['ScienceDirect'] = []
self.nextround_urls['IEEE'] = []
self.nextround_urls['Springer'] = []
self.nextround_urls['ACM'] = []
def sortUrl(self, url):
ispdf = re.findall(r'.pdf$', url)
if ispdf:
return 'other'
sort = re.findall(r'www\.sciencedirect\.com', url)
if sort:
return 'ScienceDirect'
sort = re.findall(r'ieeexplore\.ieee\.org', url)
if sort:
return 'IEEE'
sort = re.findall(r'link\.springer\.com', url)
if sort:
return 'Springer'
sort = re.findall(r'dl\.acm\.org', url)
if sort:
return 'ACM'
return 'other'
def generateSpider(self, db, url):
if db == 'ScienceDirect':
referencespider = ScienceDirectSpider(url)
return referencespider
if db == 'IEEE':
referencespider = IEEESpider(url)
return referencespider
if db == 'Springer':
referencespider = SpringerSpider(url)
return referencespider
if db == 'ACM':
referencespider = ACMSpider(url)
return referencespider
def getFirstRoundUrls(self):
url_base = self.spiderconfig['googlescholar_base']
url_base += '&q=' + self.spiderconfig['keyword']
url_base += '&start='
isfull = 0
pagenum = 0
urlnum = 0
while True:
url = url_base + str(pagenum * 10)
time.sleep(3)
response = requests.get(url, headers=self.spiderconfig['headers'], timeout=10)
page = response.content
soup = BeautifulSoup(page, 'html.parser')
gsrt_list = soup.select('.gs_rt > a')
for gsrt in gsrt_list:
if urlnum < self.spiderconfig['breadth']:
url = gsrt['href']
sort = self.sortUrl(url)
if sort != 'other':
print url
self.nextround_urls[sort].append(url)
urlnum += 1
else:
isfull = 1
break
if not gsrt_list or isfull:
break
else:
pagenum += 1
def crawl(self, request):
self.getFirstRoundUrls()
i = 0
lastroundflag = False
for crawldepth in range(1, self.spiderconfig['depth']+1):
if crawldepth == self.spiderconfig['depth']:
lastroundflag = True
thisround_urls = copy.deepcopy(self.nextround_urls)
thisround_urls = self.duplicateThisRoundUrls(thisround_urls)
self.resetNextRoundUrls()
threads = []
for db in ['ScienceDirect','IEEE','Springer','ACM']:
urls = thisround_urls[db]
for url in urls:
referencespider = self.generateSpider(db, url)
thread = ReferenceSpiderThread(referencespider, lastroundflag)
i += 1
thread.start()
threads.append(thread)
thread.join()
senddata = str(str(i) + '-' + referencespider.crawlresult['title'])
print senddata
# request.websocket.send(senddata)
for thread in threads:
thread.join()
referurls = []
for thread in threads:
referurls.extend(thread.referurls)
for referurl in referurls:
referurl = referurl
sort = self.sortUrl(referurl)
if sort != 'other':
self.nextround_urls[sort].append(referurl)
def webcrawl(request, keyword, breadth, depth):
import sys
reload(sys)
sys.setdefaultencoding('utf8')
spiderconfig = SpiderConfig()
spiderconfig.set_config('googlescholar_base', 'https://c3.glgooo.top/scholar?hl=zh-CN')
spiderconfig.set_config('keyword', keyword)
spiderconfig.set_config('breadth', breadth)
spiderconfig.set_config('depth', depth)
spider = GoogleScholarSpider(spiderconfig)
spider.crawl(request)
if __name__ == '__main__':
import sys
reload(sys)
sys.setdefaultencoding('utf8')
spiderconfig = SpiderConfig()
spiderconfig.set_config('googlescholar_base', 'https://c3.glgooo.top/scholar?hl=zh-CN')
spiderconfig.set_config('keyword', 'fire')
spiderconfig.set_config('breadth', 3)
spiderconfig.set_config('depth', 2)
spider = GoogleScholarSpider(spiderconfig)
spider.crawl(1)