-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_scholar_spider.py
251 lines (188 loc) · 6.49 KB
/
google_scholar_spider.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
版权声明:本文为CSDN博主「嘿嘿嘿唔哈哈」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Liane_Y/java/article/details/52058029
"""
import urllib.parse
import urllib.request
import re
keyword=input("keywords is?\n")
print(keyword)
url='https://scholar.google.com/scholar?&hl=en&q='+keyword+'&btnG=&lr='
header_dict={'Host': 'scholar.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Referer': 'https://scholar.google.com/schhp?hl=zh-CN',
'Connection': 'keep-alive'}
req = urllib.request.Request(url=url,headers=header_dict)
response = urllib.request.urlopen(req,timeout=120)
#print(f.read())
#with open('aaa.html', 'wb') as f:
# f.write(response.read())
print("conneect succeed!")
'''data=response.read().decode('utf-8')
pattern = re.compile(r'<div class="gs_r"><div class="gs_ri"><h3.*?<a onclick',re.S)
for m in re.finditer(pattern,data):
print (m.group())
'''
#print(response.read())
data=response.read()
data=data.decode()
pattern = re.compile(r'<div class="gs_ri">.*?</div></div></div>')
#print(data)
# 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
result1 = re.search(pattern,data)
'''
if result1:
# 使用Match获得分组信息
print (result1.group().encode('utf_8'))
else:
print ('1匹配失败!')
'''
m=re.findall(pattern,data)
print("data get")
print(len(m))
address = re.compile(r'<a href=".*?"')
author= re.compile(r'<div class="gs_a">.*?</div>')
abstruct=re.compile(r'<div class="gs_rs">.*?</div>')
for s in m:
net=re.search(address,s)
temp=net.group()
print("url:")
print(temp[9:-1])
net=re.search(author,s)
temp=net.group()
a1 = re.compile(r'<a.*?>')
print("author:")
#replacedStr = re.sub("\d+", "222", inputStr)
temp= re.sub(a1,'',temp)
print(temp[18:-6])
net=re.search(abstruct,s)
if(net):
print("abstruct:")
temp=net.group()
temp=temp.replace("<b>"," ").replace("<br>"," ").replace("</b>"," ")
print(temp[19:-6])
else:
print("no abstrutct")
print('')
url='https://scholar.google.com/scholar?start=20&hl=en&q='+keyword+'234&btnG=&lr='
header_dict={'Host': 'scholar.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Referer': 'https://scholar.google.com/schhp?hl=zh-CN',
'Connection': 'keep-alive'}
req = urllib.request.Request(url=url,headers=header_dict)
response = urllib.request.urlopen(req,timeout=120)
#print(f.read())
#with open('aaa.html', 'wb') as f:
# f.write(response.read())
print("conneect succeed!")
'''data=response.read().decode('utf-8')
pattern = re.compile(r'<div class="gs_r"><div class="gs_ri"><h3.*?<a onclick',re.S)
for m in re.finditer(pattern,data):
print (m.group())
'''
#print(response.read())
data=response.read()
data=data.decode()
pattern = re.compile(r'<div class="gs_ri">.*?</div></div></div>')
#print(data)
# 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
result1 = re.search(pattern,data)
'''
if result1:
# 使用Match获得分组信息
print (result1.group().encode('utf_8'))
else:
print ('1匹配失败!')
'''
m=re.findall(pattern,data)
print("data get")
print(len(m))
address = re.compile(r'<a href=".*?"')
author= re.compile(r'<div class="gs_a">.*?</div>')
abstruct=re.compile(r'<div class="gs_rs">.*?</div>')
for s in m:
net=re.search(address,s)
temp=net.group()
print("url:")
print(temp[9:-1])
net=re.search(author,s)
temp=net.group()
a1 = re.compile(r'<a.*?>')
print("author:")
#replacedStr = re.sub("\d+", "222", inputStr)
temp= re.sub(a1,'',temp)
print(temp[18:-6])
net=re.search(abstruct,s)
if(net):
print("abstruct:")
temp=net.group()
temp=temp.replace("<b>"," ").replace("<br>"," ").replace("</b>"," ")
print(temp[19:-6])
else:
print("no abstrutct")
print('')
start=20
start+=10
url='https://scholar.google.com/scholar?start='+str(start)+'&hl=en&q='+keyword+'234&btnG=&lr='
header_dict={'Host': 'scholar.google.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Referer': 'https://scholar.google.com/schhp?hl=zh-CN',
'Connection': 'keep-alive'}
req = urllib.request.Request(url=url,headers=header_dict)
response = urllib.request.urlopen(req,timeout=120)
#print(f.read())
#with open('aaa.html', 'wb') as f:
# f.write(response.read())
print("conneect succeed!")
'''data=response.read().decode('utf-8')
pattern = re.compile(r'<div class="gs_r"><div class="gs_ri"><h3.*?<a onclick',re.S)
for m in re.finditer(pattern,data):
print (m.group())
'''
#print(response.read())
data=response.read()
data=data.decode()
pattern = re.compile(r'<div class="gs_ri">.*?</div></div></div>')
#print(data)
# 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
result1 = re.search(pattern,data)
'''
if result1:
# 使用Match获得分组信息
print (result1.group().encode('utf_8'))
else:
print ('1匹配失败!')
'''
m=re.findall(pattern,data)
print("data get")
print(len(m))
address = re.compile(r'<a href=".*?"')
author= re.compile(r'<div class="gs_a">.*?</div>')
abstruct=re.compile(r'<div class="gs_rs">.*?</div>')
for s in m:
net=re.search(address,s)
temp=net.group()
print("url:")
print(temp[9:-1])
net=re.search(author,s)
temp=net.group()
a1 = re.compile(r'<a.*?>')
print("author:")
#replacedStr = re.sub("\d+", "222", inputStr)
temp= re.sub(a1,'',temp)
print(temp[18:-6])
net=re.search(abstruct,s)
if(net):
print("abstruct:")
temp=net.group()
temp=temp.replace("<b>"," ").replace("<br>"," ").replace("</b>"," ")
print(temp[19:-6])
else:
print("no abstrutct")
print('')