-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraping_utils.py
341 lines (305 loc) · 9.62 KB
/
scraping_utils.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# import argparse
import csv
# import datetime
# import difflib
# import os
# import pprint
import re
import time
# import timeit
# import warnings
from time import sleep
# import matplotlib.pyplot as plt
# import matplotlib.ticker as ticker
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
# from sklearn.linear_model import LinearRegression
def make_url(keyword, conf, author, year, paper_id=None):
"""make url for search papers
normal search (keyword, conf, author, year) or target search (paper_id)
:param keyword: str or None
:param conf: str or None, conference information
:param author: str or None, author information
:param year: int or None, published year
:param paper_id: None or int, paper information
:return: url
"""
assert (
keyword is not None
or conf is not None
or author is not None
or year is not None
or paper_id is not None
), "KeywordNotFoundError"
url = "https://scholar.google.co.jp/scholar?"
if paper_id is not None:
url += f"&cites={paper_id}"
else:
url += "&as_sdt=0%2C5"
if keyword is not None:
url += f"&as_q={'%20'.join(keyword.split())}"
else:
url += "&as_q="
if conf is not None:
url += f"&as_publication={'%20'.join(conf.split())}"
if author is not None:
author = "+".join(author.split())
url += f"&as_sauthors={'%20'.join(author.split())}"
if year is not None:
url += f"&as_ylo={year}"
return url
def get_snippet(soup):
"""obtain snippet from soup
:param soup: parsed html by BeautifulSoup
:return: snippet_list
"""
tags = soup.find_all("div", {"class": "gs_rs"})
snippet_list = [tags[i].text for i in range(len(tags))]
return snippet_list
def get_title_and_url(soup):
"""obtain title and url from soup
:param soup: parsed html by BeautifulSoup
:return: title_list, url_list
"""
tags1 = soup.find_all("h3", {"class": "gs_rt"})
title_list = []
url_list = []
for tag1 in tags1:
# タイトル取得
# PDF, 書籍, B, HTML, 引用, Cのタグを除去
title = re.sub(r"\[(PDF|書籍|B|HTML|引用|C)\]", "", tag1.text)
# 空白区切りを廃止
title = "_".join(title.split(" "))
if title[0] == "_":
title = title[1:]
title_list.append(title)
# url取得
try:
url = tag1.select("a")[0].get("href")
url_list.append(url)
except IndexError:
url_list.append(None)
return title_list, url_list
def get_writer_and_year(soup):
"""obtain writer(author) and year from soup
:param soup: parsed html by BeautifulSoup
:return: writer_list, year_list
"""
tags2 = soup.find_all("div", {"class": "gs_a"})
writer_list = []
year_list = []
for tag2 in tags2:
# 著者取得
"""
writer = tag2.text
writer = re.sub(r"\d", "", writer)
for char in range(0, len(writer)):
if writer[char] == "-":
writer = writer[2 : char - 1]
break
"""
writer = tag2.text.split("\xa0- ")[0]
writer_list.append(writer)
# 論文発行年取得
year = tag2.text
year = re.sub(r"\D", "", year)
# yearが5桁以上だった場合の例外処理
if len(year) > 4:
year_list.append(year[len(year) - 4 : len(year)])
else:
year_list.append(year)
return writer_list, year_list
def get_citations(soup):
"""obtain number of citations from soup
:param soup: parsed html by BeautifulSoup
:return: ci_num_list
"""
tags3 = soup.find_all(text=re.compile("引用元"))
ci_num_list = []
for tag3 in tags3:
# 被引用数取得
citation = tag3.replace("引用元", "")
ci_num_list.append(int(citation))
return ci_num_list
def get_id(soup):
"""obtain paper id from soup
:param soup: parsed html by BeautifulSoup
:return: ci_num_list
"""
tags4 = soup.find_all("div", {"class": "gs_fl"})
p_id_list = []
for tag4 in tags4:
# 論文ID取得
try:
elem = tag4.find_all("a")[2]["href"]
a = 15
while True:
if elem[a] == "&":
break
a += 1
p_id_list.append(elem[15:a])
except:
print("")
return p_id_list
def year_list_to_cite_years(year_list,p_year):
"""convert year_list into cite_years
:param year_list,p_year:
:return: cite_years
"""
year_list_int = []
for s in year_list:
try:
year_list_int.append(int(s))
except:
pass
y = [p_year+i for i in range(2021 - p_year + 1)]
cite_years = [0 for _ in range(2021 - p_year + 1)]
for year in year_list_int:
if year >= p_year and year <= 2021:
cite_years[year - p_year] += 1
list_return = [y, cite_years]
# cite_years = pd.DataFrame(cite_years,
# index=y,
# columns=['total'])
# cite_years = cite_years.T
return list_return
def grep_candidate_papers(url):
"""scrape first 10 papers and choose one
:param url:
:return: target paper information (title, writer, year, citations, url, paper_id, snippet)
"""
html_doc = requests.get(url).text
soup = BeautifulSoup(html_doc, "html.parser")
title_list, url_list = get_title_and_url(soup)
writer_list, year_list = get_writer_and_year(soup)
ci_num_list = get_citations(soup)
p_id_list = get_id(soup)
snippet_list = get_snippet(soup)
for i in range(len(title_list)):
print("-" * 20)
print(f"paper number: {str(i)}")
print(f"paper title: {title_list[i]}")
print(f"published year: {year_list[i]}")
print(f"citations: {ci_num_list[i]}")
target_paper_num = -1
while target_paper_num < 0 or target_paper_num >= len(title_list):
target_paper_num = int(input("Select paper number: "))
if target_paper_num < 0 or target_paper_num >= len(title_list):
print("Index out of range! Please re-enter")
target_paper = {
"title": title_list[target_paper_num],
"writer": writer_list[target_paper_num],
"year": year_list[target_paper_num],
"citations": ci_num_list[target_paper_num],
"url": url_list[target_paper_num],
"paper_id": p_id_list[target_paper_num],
"snippet": snippet_list[target_paper_num],
}
return target_paper
def scraping_papers(url):
"""scrape 100 papers
:param url: target url
:return: title_list, url_list, writer_list, year_list, ci_num_list, p_id_list, snippet_list
"""
url_each = url.split("&")
url_each[0] = url_each[0] + "start={}"
url_base = "&".join(url_each)
title_list = []
url_list = []
writer_list = []
year_list = []
ci_num_list = []
p_id_list = []
snippet_list = []
for page in range(0, 100, 10):
print("Loading next {} results".format(page + 10))
url_tmp = url_base.format(page)
html_doc = requests.get(url_tmp).text
soup = BeautifulSoup(html_doc, "html.parser")
title_list_tmp, url_list_tmp = get_title_and_url(soup)
writer_list_tmp, year_list_tmp = get_writer_and_year(soup)
ci_num_list_tmp = get_citations(soup)
p_id_list_tmp = get_id(soup)
snippet_list_tmp = get_snippet(soup)
title_list.extend(title_list_tmp)
url_list.extend(url_list_tmp)
writer_list.extend(writer_list_tmp)
year_list.extend(year_list_tmp)
ci_num_list.extend(ci_num_list_tmp)
p_id_list.extend(p_id_list_tmp)
snippet_list.extend(snippet_list_tmp)
sleep(np.random.randint(5, 10))
return (
title_list,
url_list,
writer_list,
year_list,
ci_num_list,
p_id_list,
snippet_list,
)
def write_csv(
conf,
title_list,
url_list,
writer_list,
year_list,
ci_num_list,
p_id_list,
snippet_list,
):
"""write csv
:param conf, title_list, url_list, writer_list, year_list, ci_num_list, snippet_list:
:return:
"""
labels = [
"conference",
"title",
"writer",
"year",
"citations",
"url",
"paper ID",
"snippet",
]
path = "data/conf_csv/" + conf + ".csv"
with open(path, "w") as f:
csv_writer = csv.writer(f)
csv_writer.writerow(labels)
for title, url, writer, year, ci_num, p_id, snippet in zip(
title_list,
url_list,
writer_list,
year_list,
ci_num_list,
p_id_list,
snippet_list,
):
csv_writer.writerow([conf, title, writer, year, ci_num, url, p_id, snippet])
if __name__ == "__main__":
#conf = "ICASSP"
conf = 'arxiv'
keyword = "pretraining bert"
year = "2018"
url = make_url(keyword=keyword, conf=conf, author=None, year=year)
print(f"url: {url}")
# select target paper
target_paper = grep_candidate_papers(url)
print(f"target paper: {target_paper}")
# create paper list about target paper's citation
url_cite = make_url(
keyword=None, conf=None, author=None, year=None, paper_id=target_paper["paper_id"]
)
(
title_list,
url_list,
writer_list,
year_list,
ci_num_list,
p_id_list,
snippet_list,
) = scraping_papers(url_cite)
cite_year = year_list_to_cite_years(year_list,int(target_paper['year']))
print(cite_year)