-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscrappaper.py
241 lines (166 loc) · 6.62 KB
/
scrappaper.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
'''
==================================
'ScrapPaper' by M. R. Rafsanjani
==================================
A web scraping method to extract titles, links and citations from PubMed's and Google's Scholar search results primarily for meta-analysis use.
Thank you for using, and share the knowledge. Please read disclamer on the origial paper or Github prior use.
Refer the paper for guide and tutorial, and cite if you found this program useful.
Completed on 2022 Feb 13th, Penang, Malaysia.
'''
print("Initiating... please wait.\n")
import requests
import csv
import re
import random
import time
import pandas as pd
from sys import exit
from bs4 import BeautifulSoup
# ===== DEFINE FUNCTIONS =====
search_from, URL_edit= "", ""
def wait():
print("Waiting for a few secs...")
time.sleep(random.randrange(1, 6))
print("Waiting done. Continuing...\n")
def checkPage():
global search_from
if "scholar.google.com" in URL_input:
search_from = "Google Scholar"
print("Input is from: Google Scholar.\n")
elif "pubmed" in URL_input:
search_from = "Pubmed"
print("Input is from: PubMed.\n")
else:
print("Page URL undefined.\n")
# ===== GETTING AND SETTING THE URL =====
URL_input = input("Please paste search URL and press Enter:")
URL_ori = URL_input
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/15.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20210916 Firefox/95.0',
})
checkPage()
# ===== MAIN FRAMEWORK =====
# ===== CODE FOR PUBMED =====
if search_from == "Pubmed":
try:
# SETTING UP THE CSV FILE
outfile = open("scrapped_pubmed.csv","w",newline='',encoding='utf-8')
writer = csv.writer(outfile)
df = pd.DataFrame(columns=['Title','Links','References'])
# SETTING & GETTING PAGE NUMBER
page_num = 1
page_view = 100 # can be change to 10, 20, 50, 100 or 200
URL_edit = URL_ori + "&page=" + str(page_num) + "&size=" + str(page_view)
print("URL : ", URL_edit)
page = requests.get(URL_edit, headers=headers, timeout=None)
soup = BeautifulSoup(page.content, "html.parser")
wait()
page_total = soup.find("label", class_="of-total-pages").text
page_total_num = int(''.join(filter(str.isdigit, page_total)))
print(f"Total page number: {page_total_num}")
print(f"Results per page: {page_view}.\n")
except AttributeError:
print("Opss! ReCaptcha is probably preventing the code from running.")
print("Please consider running in another time.\n")
exit()
wait()
# EXTRACTING INFORMATION
for i in range(page_total_num):
page_num_up = page_num + i
URL_edit = URL_ori + "&page=" + str(page_num_up) + "&size=" + str(page_view)
page = requests.get(URL_edit, headers=headers, timeout=None)
soup = BeautifulSoup(page.content, "html.parser")
wait()
results = soup.find("section", class_="search-results-list")
try:
# EXTRACTING INFORMATION
job_elements = results.find_all("article", class_="full-docsum")
for job_element in job_elements:
title_element = job_element.find("a", class_="docsum-title")
cit_element = job_element.find("span", class_="docsum-journal-citation full-journal-citation").text.strip()
links = job_element.find_all("a")
for link in links:
link_url = link["href"]
title_element_clean = title_element.text.strip()
link_url_clean = "https://pubmed.ncbi.nlm.nih.gov"+link_url
print(title_element_clean)
print(link_url_clean)
print(cit_element)
print()
df2 = pd.DataFrame([[title_element_clean, link_url_clean, cit_element]],columns=['Title','Links','References'])
df = pd.concat([df, df2], ignore_index=True)
wait()
except AttributeError:
print("Opss! ReCaptcha is probably preventing the code from running.")
print("Please consider running in another time.\n")
exit()
df.index += 1
df.to_csv('scrapped_pubmed.csv')
outfile.close()
# ===== CODE FOR GOOGLE SCHOLAR =====
elif search_from == "Google Scholar":
try:
# SETTING UP THE CSV FILE
outfile = open("scrapped_gscholar.csv","w",newline='',encoding='utf-8')
writer = csv.writer(outfile)
df = pd.DataFrame(columns=['Title','Links','References'])
# SETTING & GETTING PAGE NUMBER
page_num = 0
URL_edit = str(URL_ori + "&start=" + str(page_num))
page = requests.get(URL_edit, headers=headers, timeout=None)
soup = BeautifulSoup(page.content, "html.parser")
wait()
search_results = soup.find_all("div", class_="gs_ab_mdw")[1].text
if "About" in search_results:
search_results_split = search_results.split("results")[0].split("About")[1]
elif "results" in search_results:
search_results_split = search_results.split("results")[0]
else:
search_results_split = search_results.split("result")[0]
search_results_num = int(''.join(filter(str.isdigit, search_results_split)))
page_total_num = int(search_results_num / 10) + 1
print(f"Total page number: {page_total_num}")
print(f"Total search results: {search_results_num}.\n")
except AttributeError:
print("Opss! ReCaptcha is probably preventing the code from running.")
print("Please consider running in another time.\n")
exit()
wait()
# EXTRACTING INFORMATION
for i in range(page_total_num):
# SETTING UP URL SECOND TIME
page_num_up = page_num + i
print(f"Going to page {page_num_up}.\n")
URL_edit = str(URL_ori + "&start=" + str(page_num_up) + "0")
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/15.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20210916 Firefox/95.0',
})
page = requests.get(URL_edit, headers=headers, timeout=None)
soup = BeautifulSoup(page.content, "html.parser")
wait()
results = soup.find("div", id="gs_res_ccl_mid")
# EXTRACTING INFORMATION
try:
job_elements = results.find_all("div", class_="gs_ri")
for job_element in job_elements:
ref_element = job_element.find("div", class_="gs_a").text
links = job_element.find("a")
link_url = links["href"]
title_element = links.text.strip()
print(title_element)
print(link_url)
print(ref_element)
print()
df2 = pd.DataFrame([[title_element, link_url, ref_element]], columns=['Title','Links','References'])
df = pd.concat([df, df2], ignore_index=True)
except AttributeError:
print("Opss! ReCaptcha is probably preventing the code from running.")
print("Please consider running in another time.\n")
exit()
df.index += 1
df.to_csv('scrapped_gscholar.csv',encoding='utf-8')
outfile.close()
# END OF PROGRAM
print("Job finished, Godspeed you! Cite us.")