def get_questions_for_query(query, count=10):
"""
Fetch questions for a query using stackoverflow default search mechanism.
Returned question urls are relative to SO homepage.
At most 10 questions are returned. (Can be altered by passing count)
:param query: User-entered query string
:return: list of [ (question_text, question_description, question_url) ]
"""
questions = []
randomheaders()
search_res = requests.get(soqurl + query, headers=header)
captchacheck(search_res.url)
soup = BeautifulSoup(search_res.text, 'html.parser')
try:
soup.find_all("div", class_="question-summary")[0] # For explicitly raising exception
except IndexError:
print_warning("No results found...")
sys.exit(0)
tmp = (soup.find_all("div", class_="question-summary"))
tmp1 = (soup.find_all("div", class_="excerpt"))
i = 0
while (i < len(tmp)):
if i == count: break # limiting results
question_text = ' '.join((tmp[i].a.get_text()).split())
question_text = question_text.replace("Q: ", "")
question_desc = (tmp1[i].get_text()).replace("'\r\n", "")
question_desc = ' '.join(question_desc.split())
question_local_url = tmp[i].a.get("href")
questions.append((question_text, question_desc, question_local_url))
i = i + 1
return questions
评论列表
文章目录