-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
57 lines (48 loc) · 1.73 KB
/
bot.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
from bs4 import BeautifulSoup
import requests
base_url = "http://questions.menstrupedia.com"
def make_query():
user_query = input ("What do you want to know regarding menstrutation: ")
query = user_query.split(" ")
query = "+".join(query)
return query
def fetch_questions(query):
url = base_url + "/search/?q="+query+"&Submit=search&t=question"
page = requests.get(url)
data = BeautifulSoup(page.content, "html.parser")
ques = data.findAll(class_="question-summary-wrapper")
c=0
url_list = []
if len(ques) == 0:
print ("No questions found")
for i in ques:
if c == 5:
break
c+=1
Q = i.find("h2").get_text()
print(str(c)+". "+ Q)
h = i.find("a")
print(h["title"]+"\n")
ans_url = (base_url + h["href"]+"\n")
url_list.append(ans_url)
print("\n$$$*****************************************************************************$$$\n")
return url_list
def fetch_answer(url_list):
user_ans = int(input("How many answers you would like to have (max=5): "))
ans_page = requests.get(url_list[user_ans-1])
ans_data = BeautifulSoup(ans_page.content, "html.parser")
ans = ans_data.findAll(class_="answer-body")
if len(ans) == 0:
print ("No Answer found")
number_of_answers = 0
for i in ans:
if (number_of_answers==5):
break
number_of_answers += 1
A = i.find("p").get_text()
print (str(number_of_answers)+ ". " + A + "\n")
print ("\n$$$----------------------Others also answered-----------------------------------------$$$\n")
query = make_query()
question_urls = fetch_questions(query)
if len(question_urls) > 0:
fetch_answer(question_urls)