-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrap.py
More file actions
225 lines (191 loc) · 6.22 KB
/
scrap.py
File metadata and controls
225 lines (191 loc) · 6.22 KB
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
'''
data plotty
dataiku
'''
from bs4 import BeautifulSoup as bs
import requests as rq
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
headers = {"user-agent" : USER_AGENT}
def searchgoogle(topic):
'search for meaning in google'
words = topic.replace(' ','+')
url = f'https://www.google.com/search?q={words}'
try:resp = rq.get(url+'+in+computer+science', headers=headers)
except Exception as e:
return 'error: '+str(e)
if not resp.ok:
return 'error: 1'
page=bs(resp.text,'html.parser')
data=page.find(class_=['hgKElc','QIclbb'])
if data:
if data.span:
return data.div.text
else:
return data.text
try:resp = rq.get(url, headers=headers)
except Exception as e:
return 'error: '+str(e)
if not resp.ok:
return 'error: 1'
page=bs(resp.text,'html.parser')
data=page.find(class_=['hgKElc','QIclbb'])
if data:
if data.span:
return data.div.text
else:
return data.text
try:resp = rq.get(url+'+means', headers=headers)
except Exception as e:
return 'error: '+str(e)
if not resp.ok:
return 'error: 1'
page=bs(resp.text,'html.parser')
data=page.find(class_=['hgKElc','QIclbb'])
if data:
if data.span:
return data.div.text
else:
return data.text
return ''
tmp=[]
def searchgeeks(topic):
'''search for topic in geeksforgeeks'''
if topic in ['bye','exit']:
return ['bye bye']
words = topic.replace(' ','+')
url = f'https://www.google.com/search?q=geeksforgeeks+{words}'
try:resp = rq.get(url, headers=headers)
except Exception as e:
return 'error: '+str(e)
if not resp.ok:
return 'error: 1'
page=bs(resp.text,'html.parser')
url2 = page.select_one('.g a')['href']
if not url2:
return 'error: 2'
resp2 = rq.get(url2)
if not resp2.ok:
return 'error: 3'
page2=bs(resp2.text,'lxml')
#title=page2.find(class_='entry-header')
title=page2.h1
data=page2.find(class_='content')
if data:
data=data.find(class_='text')
tmp.append(data)
if not data:
return 'error: Not found in geeksforgeeks'
while data.find(class_='_ap_apex_ad'):
data.find(class_='_ap_apex_ad').decompose()
code = data.find(class_='code-block')
if code:
code.decompose()
code=data.find(class_='responsive-tabs')
if code:
code.decompose()
if title.time:
title.time.decompose()
ans=[]
if title and 'entry-content' not in title.parent['class']:
ans.append(str(title)) #str(title) for html code
for i in data.children:
if i.name and (len(str(i))>10 or (i.children.__length_hint__()==1 and i.findNext().name=='img')):
flag=0
for each in ('Recommended','Following are','write comments','Attention reader'):
if i.text.find(each)!=-1:
flag=1
if flag==1:
break
if i.text:
ans.append(i.decode()) #str(i) for html code
'''
if len(ans)==2:
ans.append('\nfor more go to the link: '+url2)
break
'''
return ''.join(ans)
def cppcode(topic):
'''search for topic's code in cp-algorithms'''
#global page2, code
if topic in ['bye','exit']:
return ['bye bye']
words = topic.replace(' ','+')
url = f'https://www.google.com/search?q=cp-algorithms+{words}'
try:resp = rq.get(url, headers=headers)
except Exception as e:
return 'error: '+str(e)
if not resp.ok:
return 'error: 1'
page=bs(resp.text,'html.parser')
url2 = page.select_one('.g a')['href']
if not url2:
return 'error: 2'
resp2 = rq.get(url2)
if not resp2.ok:
return 'error: 3'
page2=bs(resp2.text,'html.parser')
h2=page2.findAll(('h2','h3'))
for imp in h2:
if imp.text.find('Implementation')!=-1:
code=''
while imp and imp.name!='pre':
if imp.nextSibling.name:
code+=str(imp.nextSibling) #str(imp.nextSibling) for html code
imp=imp.nextSibling
code = str(page2.h1)+code
break
else:
if page2.pre:
code = str(page2.h1)+ str(page2.pre.previous.previous)+'\n'+ str(page2.pre)
else:
code = None
return code
if __name__=='__main__':
while True:
ques = input('user: ').strip().lower()
#extract 'topic' to search from user input, to be done
topic = ques
if topic in ['bye','exit']:
print('bye bye')
break
#searching topic in google
ans = searchgoogle(topic)
print(ans)
#searching topic in geeksforgeeks
ans = searchgeeks(topic)
if type(ans)==list:
print(*ans)
if ans[0]=='bye bye':
break
'''# prompting user in every 400 letters.
size=0
for i in ans:
if size>400:
size=0
if input('would you like to know more(y/n): ').lower() in ('n','no'):
break
print(i)
size+=len(i)
'''
#search for topic's code in cp-algorithms
code = cppcode(topic)
if code:
print('\n\n', code)
else:
print(ans) #error messege
'''
errors:
dp => <h3> recent articles </h3> <ul>.. #at end
array => <h3><a href="...">Recent articles on Arrays </a></h3> #at start
<p><strong>Topics :</strong></p>
loading image explanations.
loading links on text
#errors removed
<p>Please write comments if...
<div id="personalNoteDiv" class="clear hideIt">
<p>See <a href="..">this post</a> for all applications of Depth First Traversal.<br>
Following are implementations of simple Depth First Traversal...
<div class="recommendedPostsDiv">
<div id="practiceLinkDiv"><h2><a href="https://practice.geeksforgeeks.org/problems/
bfs-traversal-of-graph/1">Recommended: Please solve it on “<b><i><u>PRACTICE< ...
'''