forked from ab-anand/Automation-Bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quora.py
61 lines (50 loc) · 2.02 KB
/
quora.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
import os
import webbrowser
from colorama import Fore, Back, Style
from colorama import init
init()
os.environ["HTTPS_PROXY"] = "http://username:[email protected]:3128"
import requests
from bs4 import BeautifulSoup
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
#Take query and convert into search parameter
query=input('Ask your Quora que: ')
query=query.replace(' ','+')
#retrieve query
url='https://www.quora.com/search?q='+query
source_code = requests.get(url, headers=headers, timeout=15)
plain_text=source_code.text
soup=BeautifulSoup(plain_text,"html.parser")
#get the relevant questions list
que_list=soup.findAll('a',{'class':'question_link'})
hrefs=list(que_list)
#convert into user-friendly string
print(Fore.GREEN+' << Showing some relevant questions asked >>')
for i in range(len(que_list)):
que_list[i]['href']=que_list[i]['href'].replace('-',' ')
que_list[i]['href']=que_list[i]['href'].replace('/','')
print(str(i+1)+'. '+que_list[i]['href'])
print(' <-------------------------------/-------------------------------->')
get_inp=input('Select a question from the above > ')
#retrieve the page with that answer
url='https://www.quora.com/'+hrefs[int(get_inp)-1]['href'].replace(' ','-')
try:
source_code = requests.get(url, timeout=15)
plain_text=source_code.text
soup=BeautifulSoup(plain_text,"html.parser")
ans=soup.findAll('div',{'class':'AnswerHeader ContentHeader'})
header=ans[0].text
nans=ans[0].parent
mans=nans.next_sibling
#man=mans.findNextSibling()
text=mans.text
pos=text.find('Upvotes')
uf=text[0:pos+7]
print(Fore.BLUE+header)
print(uf)
except:
print('Sorry, this que hasn\'t been answered.')
print(' <----------------------------------/------------------------------->')
a=input('Head over to the link for more answers?(y/n) ')
if a is 'y':
webbrowser.open(url)