-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselci.py
More file actions
203 lines (136 loc) · 4.63 KB
/
Copy pathselci.py
File metadata and controls
203 lines (136 loc) · 4.63 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
import time
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from selenium import webdriver
driver = webdriver.Chrome("/home/mj/Documents/scrape/selscrape/chromedriver")
pages=set()
extract={}
baseurl="https://www.quora.com"
flag=1
def get_topic_list(pagenum,word):
n=len(pagenum)
temp=0
i=0
for i in range(0,n):
linktoclick=pagenum[i]
html=urlopen(linktoclick)
bsobj=BeautifulSoup(html)
maindiv=bsobj.find("div",{"class":"ContentWrapper"})
ls=maindiv.find("a",href=re.compile("(/topic/)"))
if word.lower()>ls.get_text().lower():
temp=i
elif word.lower()<ls.get_text().lower() and word.lower() not in ls.get_text().lower():
flag=-1
break
if i==temp:
try:
linktoclick=pagenum[i]
html=urlopen(linktoclick)
bsobj=BeautifulSoup(html)
maindiv=bsobj.find("div",{"class":"ContentWrapper"})
ls=maindiv.findAll("a",href=re.compile("(/topic/)"))
if word.lower()>ls[0].get_text().lower() and word.lower()<=ls[len(ls)-1].get_text().lower() :
for k in ls :
m= re.search( r'topic/(.*)',k.attrs["href"], re.M|re.I)
m=m.group(1)
extract[k.get_text().lower()]=m
except:
pass
else:
for j in range(temp,i):
linktoclick=pagenum[j]
html=urlopen(linktoclick)
bsobj=BeautifulSoup(html)
maindiv=bsobj.find("div",{"class":"ContentWrapper"})
ls=maindiv.findAll("a",href=re.compile("(/topic/)"))
for k in ls :
m= re.search( r'topic/(.*)',k.attrs["href"], re.M|re.I)
m=m.group(1)
extract[k.get_text().lower()]=m
def begin(pageUrl,word):
pagenum=[]
linktoclick=baseurl+pageUrl+word[:2].lower()
pagenum.append(linktoclick)
while 1:
html=urlopen(linktoclick)
bsobj=BeautifulSoup(html)
maindiv=bsobj.find("div",{"class":"ContentWrapper"})
pages_head=maindiv.find("h2")
cur=pages_head.find("strong").get_text()
pagen=pages_head.findAll("a")
for i in pagen:
if i.get_text()!="Next" and i.get_text()!="Previous" and int(i.get_text())>int(cur) :
pagenum.append(baseurl+i.attrs["href"])
get_topic_list(pagenum,word)
if (pagen[len(pagen)-1].get_text()!="Next" or flag==-1):
return extract
linktoclick=pagenum[len(pagenum)-1]
driver.get(linktoclick)
pagenum=[]
def binary_search(word,ls):
l=0;
r=len(ls)-1
mid=l+(r-l)//2
while l<=r:
if ls[mid].lower()==word:
return mid
elif ls[mid].lower()>word:
r=mid-1
else:
l=mid+1
mid=l+(r-l)//2
return -1
# ,href=re.compile("(/{wo}/)".format(wo=word))
ans={}
ansl=[]
def getLinks(pageUrl,word):
k=0
global pages
html=urlopen(pageUrl)
bsObj=BeautifulSoup(html)
for link in bsObj.findAll("a",{"class":"question_link"}):
if 'href' in link.attrs:
if link.attrs['href'] not in pages:
newPage=link.attrs['href']
pages.add(newPage)
ansl.append(newPage)
# for link in bsObj.findAll("span",{"class":"count"}):
# ans[ansl[k]]=link.get_text()
# k=k+1
def go_to_topic(word):
word=word.lower()
di=begin("/sitemap/alphabetical_topics/",word)
ls=sorted(di.keys())
ind=binary_search(word,ls)
if ind!=-1:
topic_url=di[ls[ind]]
linktoclick="https://www.quora.com/topic/{t}".format(t=topic_url)
driver.get(linktoclick)
getLinks(linktoclick,word)
# for i in ans.keys():
# print(i,"--->",ans[i],"\n\n")
for i in ansl:
print(baseurl+i,"\n")
else:
print("Not found!!\n\nSuggested Searches:\n\n")
for i in ls:
print(i,"\n")
go_to_topic("data")
# # driver function
# def main():
# # Input keys (use only 'a' through 'z' and lower case)
# keys = ["the","a","there","anaswe","any",
# "by","their"]
# output = ["Not present in trie",
# "Present in tire"]
# # Trie object
# t = Trie()
# # Construct trie
# for key in keys:
# t.insert(key)
# # Search for different keys
# print("{} ---- {}".format("the",output[t.search("the")]))
# print("{} ---- {}".format("these",output[t.search("these")]))
# print("{} ---- {}".format("their",output[t.search("their")]))
# print("{} ---- {}".format("thaw",output[t.search("thaw")]))