Using the Bing XML Search API with Ruby

Posted by Adrian Mon, 13 Jul 2009 09:43:00 GMT

This code demonstrates how you can use the Bing XML api to generate Bing search results for your Ruby (and Ruby on Rails) application

Before you can run this code, you need to register as a Bing developer and create an AppId for your application. This is quick and completely free. See http://www.bing.com/developers/ for more information.

require 'net/http'
require 'rexml/document'

appId = "1234567890123456789012345678901234567890" # Paste your real AppId here!
search = "Lewis Hamilton"

uri = URI.parse("http://api.search.live.net/xml.aspx?AppId=#{appId}&sources=Web&query=#{URI.escape(search)}")
xml_response = Net::HTTP.get_response(uri).body
doc = REXML::Document.new(xml_response)

doc.elements.each("SearchResponse/web:Web/web:Results/web:WebResult") do |result| 
  
  p result.get_elements("web:Title")[0].text
  p result.get_elements("web:Description")[0].text
  p result.get_elements("web:Url")[0].text
  p result.get_elements("web:DisplayUrl")[0].text
  p result.get_elements("web:DateTime")[0].text
  p "--------------------------------------------------" 
 
end

Related: Bing: Performing a basic web search over SOAP with the new Bing API 2.0


About

We are a small British company that produces business-oriented software and solutions. These articles are a product of our daily work - information that we think might be useful to share. We hope you find them useful.

Our Software

These are some of our products. Several are open source, some are web-based and others are proprietary:

Categories

Archives

Syndicate

ml> ._trackPageview(); } catch(err) {} ml> l> pageTracker._trackPageview(); } catch(err) {} ml> ._trackPageview(); } catch(err) {} ml> l>