-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb crawling (google schoolar).py
164 lines (122 loc) · 5.47 KB
/
web crawling (google schoolar).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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 00:02:36 2019
@author: mohamed
"""
import requests, webbrowser,urllib, time
from bs4 import BeautifulSoup
url = []
opened_pages = []
page = 10
def main(url,page):
'''openening the first 10 files in a google schoolar Url'''
def opener(com_url_pages,soup):
'''openning the the researches in the first page'''
for i in soup.find_all(id ='gs_res_ccl_mid'):
for p in i.find_all('a'):
if p.get('href').startswith('http'):
if p.get('href').endswith('.pdf'):
print ('\nthere are some pdf files\n ', p.get('href')) # prints the link for PDFs
else:
time.sleep(5)
webbrowser.open(p.get('href'))
'''openning the rest of pages upon user request'''
page_number = 2
for i in com_url_pages:
open_next = input('do you want to see the next page (page: {})?\n\
1 for yes\n\
2 for no\n'.format(page_number))
if open_next =='1':
page_number+=1
pass
elif open_next =='2':
return open_next
r = requests.get(i).text
soup = BeautifulSoup(r,'html.parser')
for i in soup.find_all(id ='gs_res_ccl_mid'):
for p in i.find_all('a'):
if p.get('href').startswith('http'):
if p.get('href').endswith('.pdf'):
print ('\nthere are some pdf files\n ', p.get('href'))
else:
time.sleep(5)
webbrowser.open(p.get('href'))
''' find and create a list of pages URL'''
user_input= input('please enter the google schooler URL: ')
r = requests.get(user_input).text
soup = BeautifulSoup(r,'html.parser')
num_pages = int(input('for what page you want to see the results: '))
if num_pages == 1:
for i in soup.find_all(id ='gs_res_ccl_mid'):
for p in i.find_all('a'):
if p.get('href').startswith('http'):
if p.get('href').endswith('.pdf'):
print ('\nthere are some pdf files\n ', p.get('href')) # prints the link for PDFs
else:
time.sleep(5)
webbrowser.open(p.get('href'))
return 1
if 1 < num_pages <= 10:
for i in soup.find_all(id ='gs_n'):
for p in i.find_all('a'):
url.append(p.get('href'))
set_lis_url_pages = list(set(url))
set_lis_url_pages.sort() # this list contains the URL pages (not complete URLs)
com_url_pages = []
for num in range(num_pages-1):
link = urllib.parse.urljoin('https://scholar.google.com/', set_lis_url_pages[num])
com_url_pages.append(link)
'''creating the full URL address'''
'''update: showing only a specific number of pages'''
if num_pages > 10:
for i in soup.find_all(id ='gs_n'):
for p in i.find_all('a'):
url.append(p.get('href'))
set_lis_url_pages = list(set(url))
set_lis_url_pages.sort() # this list contains the URL pages (not complete URLs)
com_url_pages = []
for num in range(len(set_lis_url_pages)):
link = urllib.parse.urljoin('https://scholar.google.com/', set_lis_url_pages[num])
com_url_pages.append(link)
repetitions = (num_pages - 10)/4
if int(repetitions) < repetitions:
repetitions = int(repetitions) + 1
for i in range(repetitions):
r = requests.get(com_url_pages[-1]).text
soup_10 = BeautifulSoup(r,'html.parser')
after_10 = []
for i in soup_10.find_all(id ='gs_n'):
for p in i.find_all('a'):
after_10.append(p.get('href'))
for url in range(len(after_10)):
link = urllib.parse.urljoin('https://scholar.google.com/', after_10[url])
if link not in com_url_pages:
com_url_pages.append(link)
else:
continue
com_url_pages = list(set(com_url_pages))
com_url_pages.sort()
dic_com_url_pages = {}
for i in com_url_pages:
num_start = i.find('=')+1
num_end = i.find('&')
dic_com_url_pages[int(i[num_start:num_end])]= i
list_of_keys = [i for i in dic_com_url_pages]
list_of_keys.sort()
for i in range(len(list_of_keys)):
com_url_pages[i] = dic_com_url_pages[list_of_keys[i]]
'''future update:
1- opening the number of pages entered only
2- dont repeat the opened research'''
#removal = len(com_url_pages) - num_pages + 1
#for i in range(removal):
# del(com_url_pages[-1])
open_next = opener(com_url_pages, soup)
return open_next
open_next = opener(com_url_pages,soup)
return open_next
while True:
open_next = main(url,page)
page+=7
if open_next == '2':
break