java - Bing API Specific domain -
i using bing api java , xml interface. downloaded 2 java files on computer ,try them , works fine , code use
import java.io.ioexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpression; import javax.xml.xpath.xpathexpressionexception; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document; import org.w3c.dom.nodelist; import org.xml.sax.saxexception; //live search api 2.0 code sample demonstrating use of //web sourcetype on xml protocol. class websample { static xpathfactory factory = null; static xpath xpath = null; static xpathexpression expr = null; public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception, xpathexpressionexception { // build request. string requesturl = buildrequest(); // send request live search service , response. document doc = getresponse(requesturl); if(doc != null) { // display response obtained live search service. displayresponse(doc); } } private static string buildrequest() { // replace following string appid received // live search developer center. string query="google"; string appid = "01aaeba23f10712783fe3d13aa4018153b89ac3c"; string requeststring = "http://api.search.live.net/xml.aspx?" // common request fields (required) + "appid=" + appid + "&query=" + query + "&sources=web" // common request fields (optional) + "&version=2.0" + "&market=en-us" + "&adult=moderate" // web-specific request fields (optional) + "&web.count=10" + "&web.offset=0" + "&web.filetype=doc" + "&web.options=disablehostcollapsing+disablequeryalterations"; return requeststring; } private static document getresponse(string requesturl) throws parserconfigurationexception, saxexception, ioexception { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); dbf.setnamespaceaware(true); document doc = null; documentbuilder db = dbf.newdocumentbuilder(); if (db != null) { doc = db.parse(requesturl); } return doc; } private static void displayresponse(document doc) throws xpathexpressionexception { factory = xpathfactory.newinstance(); xpath = factory.newxpath(); xpath.setnamespacecontext(new apinamespacecontext()); nodelist errors = (nodelist) xpath.evaluate("//api:error",doc,xpathconstants.nodeset); if(errors != null && errors.getlength() > 0 ) { // there errors in response. display error details. displayerrors(errors); } else { displayresults(doc); } } private static void displayresults(document doc) throws xpathexpressionexception { string version = (string)xpath.evaluate("//@version",doc,xpathconstants.string); string searchterms = (string)xpath.evaluate("//api:searchterms",doc,xpathconstants.string); int total = integer.parseint((string)xpath.evaluate("//web:web/web:total",doc,xpathconstants.string)); int offset = integer.parseint((string)xpath.evaluate("//web:web/web:offset",doc,xpathconstants.string)); nodelist results = (nodelist)xpath.evaluate("//web:web/web:results/web:webresult",doc,xpathconstants.nodeset); // display results header. system.out.println("live search api version " + version); system.out.println("web results " + searchterms); system.out.println("displaying " + (offset+1) + " " + (offset + results.getlength()) + " of " + total + " results "); system.out.println(); // display web results. stringbuilder builder = new stringbuilder(); for(int = 0 ; < results.getlength(); i++) { nodelist childnodes = results.item(i).getchildnodes(); (int j = 0; j < childnodes.getlength(); j++) { if(!childnodes.item(j).getlocalname().equalsignorecase("displayurl")) { string fieldname = childnodes.item(j).getlocalname(); if(fieldname.equalsignorecase("datetime")) { fieldname = "last crawled"; } builder.append(fieldname + ":" + childnodes.item(j).gettextcontent()); builder.append("\n"); } } builder.append("\n"); } system.out.println(builder.tostring()); } private static void displayerrors(nodelist errors) { system.out.println("live search api errors:"); system.out.println(); (int = 0; < errors.getlength(); i++) { nodelist childnodes = errors.item(i).getchildnodes(); (int j = 0; j < childnodes.getlength(); j++) { system.out.println(childnodes.item(j).getlocalname() + ":" + childnodes.item(j).gettextcontent()); } system.out.println(); } } } question : want results specific domain. how ? , should download libraries ?
in string query give
string query = "google%20site:www.apple.com" it should work, return result www.apple.com alone.
Comments
Post a Comment