123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ## Bing (Web)
  2. #
  3. # @website https://www.bing.com
  4. # @provide-api yes (http://datamarket.azure.com/dataset/bing/search),
  5. # max. 5000 query/month
  6. #
  7. # @using-api no (because of query limit)
  8. # @results HTML (using search portal)
  9. # @stable no (HTML can change)
  10. # @parse url, title, content
  11. #
  12. # @todo publishedDate
  13. from urllib import urlencode
  14. from cgi import escape
  15. from lxml import html
  16. from searx.engines.xpath import extract_text
  17. # engine dependent config
  18. categories = ['general']
  19. paging = True
  20. language_support = True
  21. # search-url
  22. base_url = 'https://www.bing.com/'
  23. search_string = 'search?{query}&first={offset}'
  24. # do search-request
  25. def request(query, params):
  26. offset = (params['pageno'] - 1) * 10 + 1
  27. if params['language'] == 'all':
  28. language = 'en-US'
  29. else:
  30. language = params['language'].replace('_', '-')
  31. search_path = search_string.format(
  32. query=urlencode({'q': query, 'setmkt': language}),
  33. offset=offset)
  34. params['cookies']['SRCHHPGUSR'] = \
  35. 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
  36. params['url'] = base_url + search_path
  37. return params
  38. # get response from search-request
  39. def response(resp):
  40. results = []
  41. dom = html.fromstring(resp.content)
  42. # parse results
  43. for result in dom.xpath('//div[@class="sa_cc"]'):
  44. link = result.xpath('.//h3/a')[0]
  45. url = link.attrib.get('href')
  46. title = extract_text(link)
  47. content = escape(extract_text(result.xpath('.//p')))
  48. # append result
  49. results.append({'url': url,
  50. 'title': title,
  51. 'content': content})
  52. # return results if something is found
  53. if results:
  54. return results
  55. # parse results again if nothing is found yet
  56. for result in dom.xpath('//li[@class="b_algo"]'):
  57. link = result.xpath('.//h2/a')[0]
  58. url = link.attrib.get('href')
  59. title = extract_text(link)
  60. content = escape(extract_text(result.xpath('.//p')))
  61. # append result
  62. results.append({'url': url,
  63. 'title': title,
  64. 'content': content})
  65. # return results
  66. return results