bing.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Bing (Web)
  3. @website https://www.bing.com
  4. @provide-api yes (http://datamarket.azure.com/dataset/bing/search),
  5. max. 5000 query/month
  6. @using-api no (because of query limit)
  7. @results HTML (using search portal)
  8. @stable no (HTML can change)
  9. @parse url, title, content
  10. @todo publishedDate
  11. """
  12. from lxml import html
  13. from searx.engines.xpath import extract_text
  14. from searx.url_utils import urlencode
  15. from searx.utils import match_language, gen_useragent
  16. # engine dependent config
  17. categories = ['general']
  18. paging = True
  19. language_support = True
  20. supported_languages_url = 'https://www.bing.com/account/general'
  21. language_aliases = {'zh-CN': 'zh-CHS', 'zh-TW': 'zh-CHT', 'zh-HK': 'zh-CHT'}
  22. # search-url
  23. base_url = 'https://www.bing.com/'
  24. search_string = 'search?{query}&first={offset}'
  25. # do search-request
  26. def request(query, params):
  27. offset = (params['pageno'] - 1) * 10 + 1
  28. if params['language'] == 'all':
  29. lang = 'EN'
  30. else:
  31. lang = match_language(params['language'], supported_languages, language_aliases)
  32. query = u'language:{} {}'.format(lang.split('-')[0].upper(), query.decode('utf-8')).encode('utf-8')
  33. search_path = search_string.format(
  34. query=urlencode({'q': query}),
  35. offset=offset)
  36. params['url'] = base_url + search_path
  37. params['headers']['User-Agent'] = gen_useragent('Windows NT 6.3; WOW64')
  38. return params
  39. # get response from search-request
  40. def response(resp):
  41. results = []
  42. dom = html.fromstring(resp.text)
  43. try:
  44. results.append({'number_of_results': int(dom.xpath('//span[@class="sb_count"]/text()')[0]
  45. .split()[0].replace(',', ''))})
  46. except:
  47. pass
  48. # parse results
  49. for result in dom.xpath('//div[@class="sa_cc"]'):
  50. link = result.xpath('.//h3/a')[0]
  51. url = link.attrib.get('href')
  52. title = extract_text(link)
  53. content = extract_text(result.xpath('.//p'))
  54. # append result
  55. results.append({'url': url,
  56. 'title': title,
  57. 'content': content})
  58. # parse results again if nothing is found yet
  59. for result in dom.xpath('//li[@class="b_algo"]'):
  60. link = result.xpath('.//h2/a')[0]
  61. url = link.attrib.get('href')
  62. title = extract_text(link)
  63. content = extract_text(result.xpath('.//p'))
  64. # append result
  65. results.append({'url': url,
  66. 'title': title,
  67. 'content': content})
  68. # return results
  69. return results
  70. # get supported languages from their site
  71. def _fetch_supported_languages(resp):
  72. supported_languages = []
  73. dom = html.fromstring(resp.text)
  74. options = dom.xpath('//div[@id="limit-languages"]//input')
  75. for option in options:
  76. code = option.xpath('./@id')[0].replace('_', '-')
  77. if code == 'nb':
  78. code = 'no'
  79. supported_languages.append(code)
  80. return supported_languages