How to use Bing as the search engine on my site?

梦想与她 提交于 2021-02-11 18:16:45

问题


Does Bing has an option similar to Google Custom Search or Yahoo where I can use Bing to power the search results on my site?

Couple requirements:

  • Works with an ASP.NET site (is a .NET project)
  • Host the search box and results on my own website
  • Ability to customize the look and feel of the results to match my site (Full control is ideal but I understand it's not possible with free solutions)

I did a search for Bing custom search and found this: http://www.bing.com/siteowner/ but it's not exactly what I am looking for.


回答1:


The query string Bing uses is:

http://www.bing.com/search?q=&src=IE-SearchBox&FORM=IE8SRC

(this is the template URL from the Bing search provider in IE). All you have to do is insert your search terms after the q parameter. A good way to test this is to actually execute a search and see the url in the address box of the browser:

http://www.bing.com/search?q=how+to+query+bing&src=IE-SearchBox&FORM=IE8SRC

you can drop the src and FORM parameters, Bing will just be using those for statistical purposes.

To get the results to appear in your own page, use an iframe, give it an id, and sets its src url (using javascript) to the search url you have constructed.

var frame = document.getElementById('mySearchFrame');
if (frame != null)
    frame.src = 'http://www.bing.com/search?q=' + mySearchTerms;

Note that if you want to style the page then you will have to query Bing from code behind and "scrape" the results and place them into your own page. (Or you could just send the page back but modify the contents of it before doing so, but to do this will be violating Bing's terms of use - MS supply Bing for you to use for free, but it is on their terms, which means you will not be able to delete any advertising or change the look and feel of the page - there are no free rides in this world :).




回答2:


You can sign up for site search and query Bing via jsonp and display results via javascript (exact code untested)

 function searchDone(results) {
    if(results.SearchResponse.Web.Results && results.SearchResponse.Web.Results.length > 0) {
       for (var i = 0; i < results.SearchResponse.Web.Results.length; i++) {
            result = results.SearchResponse.Web.Results[i];
            item = document.createElement('li');
            item.innerHTML = '<a href="' + result.Url + '">' + AntiXssLibrary.HtmlEncode(result.Title.replace(/\uE000/g, "").replace(/\uE001/g, "")) + '</a>' + '<blockquote>' + AntiXssLibrary.HtmlEncode(result.Description.replace(/\uE000/g, "").replace(/\uE001/g, "")) + '</blockquote>';
            // append child to document somewhere
        }
    }
 }



 var serviceURI = "http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=searchDone&sources=web&Options=EnableHighlighting";
 var appid = "&Appid=YOUR_BING_APP_ID";      
 var query = "&query=site:http://YOURDOMAIN.com/ <%=Request.Querystring["query"] %>";

 var fullUri = serviceURI + appid + query;
 var head = document.getElementsByTagName('head');
 var script = document.createElement('script');
 script.type = "text/javascript";
 script.src = fullUri;
 head[0].appendChild(script);


来源:https://stackoverflow.com/questions/3260465/how-to-use-bing-as-the-search-engine-on-my-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!