ask.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Ask.com"""
  4. from urllib.parse import urlencode
  5. import re
  6. from lxml import html
  7. # Metadata
  8. about = {
  9. "website": "https://www.ask.com/",
  10. "wikidata_id": 'Q847564',
  11. "official_api_documentation": None,
  12. "use_official_api": False,
  13. "require_api_key": False,
  14. "results": "HTML",
  15. }
  16. # Engine Configuration
  17. categories = ['general']
  18. paging = True
  19. # Base URL
  20. base_url = "https://www.ask.com/web"
  21. def request(query, params):
  22. query_params = {
  23. "q": query,
  24. "page": params["pageno"],
  25. }
  26. params["url"] = f"{base_url}?{urlencode(query_params)}"
  27. return params
  28. def response(resp):
  29. text = html.fromstring(resp.text).text_content()
  30. urls_match = re.findall(r'"url":"(.*?)"', text)
  31. titles_match = re.findall(r'"title":"(.*?)"', text)[3:]
  32. content_match = re.findall(r'"abstract":"(.*?)"', text)
  33. results = [
  34. {
  35. "url": url,
  36. "title": title,
  37. "content": content,
  38. }
  39. for url, title, content in zip(urls_match, titles_match, content_match)
  40. if "&qo=relatedSearchNarrow" not in url
  41. # Related searches shouldn't be in the search results: www.ask.com/web&q=related
  42. ]
  43. return results