-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch_answer_and_related_questions.py
263 lines (241 loc) · 9.75 KB
/
fetch_answer_and_related_questions.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
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from bs4 import BeautifulSoup
import time
import numpy as np
import re
import csv
import json
import pandas as pd
import numpy as np
import sys
import xlsxwriter
class fetch_answer_and_related_questions:
def __init__(self,topic_dir,topic):
self.f = False
self.direct_related_marker = False
self.topic_dir = topic_dir
self.topic = topic
self.base_url = "https://www.quora.com"
def fetch(self,driver,dictionary_list,count):
f = self.f
if self.direct_related_marker is True:
workbook = xlsxwriter.Workbook(str(self.topic_dir)+"/"+str(self.topic)+"till_"+str(count)+"_answers_related.xlsx")
else:
workbook = xlsxwriter.Workbook(str(self.topic_dir)+"/"+str(self.topic)+"till_"+str(count)+"_answers.xlsx")
worksheet = workbook.add_worksheet()
row = 0
for dictionary in dictionary_list:
col = 0
answer_link_list = dictionary["answer_link_list"]
answer_upvote_list = []
worksheet.write(row,col,dictionary["ques"])
print("number of answers for question number for topic "+str(self.topic)+" "+str(count)+":"+str(len(answer_link_list)))
count_a = 0
if len(answer_link_list) > 0:
for answer in answer_link_list:
url = self.base_url+answer
wait_time = np.random.uniform(0.00,1.00,size = None)
time.sleep(wait_time)
driver.get(url)
response = driver.page_source
soup_3= BeautifulSoup(response)
real_answers_list = soup_3.find_all("div",class_="ui_qtext_expanded")
if len(real_answers_list) == 0:
real_answers_list = soup_3.find_all("div",class_="ExpandedAnswer ExpandedQText")
answer_text = "no answer"
try:
for a in real_answers_list:
answer_text = str(a.find_all("span",class_="ui_qtext_rendered_qtext")[0].text)
# print(answer_text)
except Exception as e:
print(e)
if answer_text == "no answer":
pass
else:
answer_upvote_list.append({"answer":answer_text})
count_a = count_a + 1
#print("answer number "+str(count_a))
# SORT LIST OF ALL THE ANSWERS FOR EACH QUESTION ACCORDING TO THEIR UPVOTES
if len(answer_upvote_list) > 0:
sorted_list = list(reversed(answer_upvote_list))
# most_upvoted_answer = sorted_list[0]["answer"]
else:
sorted_list = [{"answer":"no_answer"}]
col = 0
# row_to_write = [dictionary["ques"]]
worksheet.write(row,col,dictionary["ques"])
for dict_ in sorted_list:
col = col + 1
# row_to_write.append(dict_["answer"])
worksheet.write(row,col,dict_["answer"])
# writer.writerow(row_to_write)
else:
worksheet.write(row,1,"no answer")
row += 1
workbook.close()
def fetch_related_questions_and_links(self):
augment_related_questions = []
augment_related_links = []
df = pd.read_excel(str(self.topic_dir)+"/"+str(self.topic)+"_links.xlsx",sheet_name="Sheet1")
questions = df.loc[:,0].values.tolist()
links = df.loc[:,1].values.tolist()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
print("topic: "+str(self.topic))
print("number_of_questions: "+str(len(questions)))
i = 0
for key,link in zip(questions,links):
if str(link).startswith("/"):
driver.get(self.base_url+""+link)
else:
driver.get(self.base_url+"/"+link)
html_source = driver.page_source
data = html_source
soup = BeautifulSoup(data)
try:
related_questions_tag = soup.find("div",class_="question_related list side_bar")
related_questions_links = related_questions_tag.find_all("a",class_='question_link')
related_questions_links = list(map(lambda x: x["href"],related_questions_links))
related_questions = related_questions_tag.find_all("span",class_='ui_qtext_rendered_qtext')
related_questions = list(map(lambda x: x.text,related_questions))
augment_related_questions.extend(related_questions)
augment_related_links.extend(related_questions_links)
except Exception as e:
print("EXCEPTION E+++ start")
print(self.topic)
print(i)
print(key)
print(link)
print(str(e)+" -- "+'Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
print("EXCEPTION E+++ end")
i +=1
print("number of related questions: "+str(len(augment_related_questions)))
array_for_both_questions_and_links = []
array_for_both_questions_and_links.append(augment_related_questions)
array_for_both_questions_and_links.append(augment_related_links)
df_questions_with_corresponding_links = pd.DataFrame(array_for_both_questions_and_links)
df_questions_with_corresponding_links = df_questions_with_corresponding_links.transpose()
df_questions_with_corresponding_links.to_csv(str(self.topic_dir)+"/"+str(self.topic)+"_links_related.csv",index=False)
driver.close()
return
def run(self):
df = pd.read_csv(str(self.topic_dir)+"/"+str(self.topic)+"_links.csv")
questions = df.loc[:,0].values.tolist()
links = df.loc[:,1].values.tolist()
df_related = pd.read_csv(str(self.topic_dir)+"/"+str(self.topic)+"_links_related.csv")
augment_related_questions = df_related.loc[:,0].values.tolist()
augment_related_links = df_related.loc[:,1].values.tolist()
# question_link is dictionary you can get from q_mf or other files
question_link ={}
len_for_differentiating_between_direct_and_related = len(questions)
questions.extend(augment_related_questions)
links.extend(augment_related_links)
questions_with_answer_links_and_views = []
count = 0
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
for key,link in zip(questions,links):
count+=1
print("\n\n\n\n\n\n\n\n\n\n\n")
print(":::::::::::::::::::::::::::::::::::::::::::::::::::"+str(count))
if self.direct_related_marker is False:
if count > len_for_differentiating_between_direct_and_related:
if len(questions_with_answer_links_and_views) > 0:
self.fetch(driver,questions_with_answer_links_and_views,count)
questions_with_answer_links_and_views = []
self.direct_related_marker = True
try:
if str(link).startswith("/"):
driver.get(self.base_url+""+link)
else:
driver.get(self.base_url+"/"+link)
html_source = driver.page_source
data = html_source
soup = BeautifulSoup(data)
any_answer = True
try:
answer_count = int(re.findall(r"\d+",str(soup.find("div",class_="answer_count")))[0])
print("answer count: "+str(answer_count))
except IndexError:
answer_count = 0
total_views = 0.0
most_upvoted_answer = "no answer"
any_answer = False
if any_answer == True:
answer_list = []
view_list = []
upvote_list = []
count_answer_fetch_complete = 0
while len(answer_list) < answer_count:
try:
old_len = len(answer_list)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
wait_time_4 = np.random.uniform(1.00,3.00,size = None)
time.sleep(wait_time_4)
html_source_scroll = driver.page_source
data_scroll = html_source_scroll
soup_scroll = BeautifulSoup(data_scroll)
answer_list = soup_scroll.find_all("a",class_='answer_permalink')
answer_list = list(map(lambda x: x["href"],answer_list))
view_list = soup_scroll.find_all("span",class_="meta_num")
new_len = len(answer_list)
if old_len == new_len:
count_answer_fetch_complete = count_answer_fetch_complete + 1
if count_answer_fetch_complete > 3:
break
except Exception as e:
print(e)
continue
total_views = 0.0
for view in view_list:
# print(view.text)
try:
if "k" in (view.text).lower():
no_of_views = float(re.findall(r'(\d+.?\d*)',view.text)[0]) * 1000
elif "m" in (view.text).lower():
no_of_views = float(re.findall(r'(\d+.?\d*)',view.text)[0]) * 1000000
elif "b" in (view.text).lower():
no_of_views = float(re.findall(r'(\d+.?\d*)',view.text)[0]) * 1000000000
else:
no_of_views = float(view.text)
except Exception as e:
print(e)
continue
total_views = total_views + no_of_views
try:
questions_with_answer_links_and_views.append({"ques":key,"answer_link_list":answer_list})
if len(questions_with_answer_links_and_views) > 10:
self.fetch(driver,questions_with_answer_links_and_views,count)
questions_with_answer_links_and_views = []
except Exception as e:
print(e)
if len(questions_with_answer_links_and_views) > 0:
self.fetch(driver,questions_with_answer_links_and_views,count)
questions_with_answer_links_and_views = []
else:
questions_with_answer_links_and_views.append({"ques":key,"answer_link_list":[]})
if len(questions_with_answer_links_and_views) > 10:
self.fetch(driver,questions_with_answer_links_and_views,count)
questions_with_answer_links_and_views = []
print("questions done so far: "+str(count))
except Exception as e:
print(str(e)+ " failed at question number count: "+str(count))
print(str(e)+" -- "+'Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
continue
driver.close()
# f = open("diabetes_answers1.csv","a")