Bing API 特定域
我目前正在使用带有 Java 和 XML 接口的 Bing API。我在计算机上下载了两个 java 文件,尝试了一下,效果很好,我使用的代码是
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 the use of the
//Web SourceType over the 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 the request.
String requestURL = BuildRequest();
// Send the request to the Live Search Service and get the response.
Document doc = GetResponse(requestURL);
if(doc != null)
{
// Display the response obtained from the Live Search Service.
DisplayResponse(doc);
}
}
private static String BuildRequest()
{
// Replace the following string with the AppId you received from the
// 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 are errors in the 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 the results header.
System.out.println("Live Search API Version " + version);
System.out.println("Web results for " + searchTerms);
System.out.println("Displaying " + (offset+1) + " to " + (offset + results.getLength()) + " of " + total + " results ");
System.out.println();
// Display the Web results.
StringBuilder builder = new StringBuilder();
for(int i = 0 ; i < results.getLength(); i++)
{
NodeList childNodes = results.item(i).getChildNodes();
for (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();
for (int i = 0; i < errors.getLength(); i++)
{
NodeList childNodes = errors.item(i).getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++)
{
System.out.println(childNodes.item(j).getLocalName() + ":" + childNodes.item(j).getTextContent());
}
System.out.println();
}
}
}
问题:我想获取特定域的所有结果。怎么办呢? 我应该下载任何库吗?
I am currently using Bing API with Java and XML Interface. I downloaded just two java files on my computer ,try them and It works fine , the code I use is
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 the use of the
//Web SourceType over the 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 the request.
String requestURL = BuildRequest();
// Send the request to the Live Search Service and get the response.
Document doc = GetResponse(requestURL);
if(doc != null)
{
// Display the response obtained from the Live Search Service.
DisplayResponse(doc);
}
}
private static String BuildRequest()
{
// Replace the following string with the AppId you received from the
// 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 are errors in the 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 the results header.
System.out.println("Live Search API Version " + version);
System.out.println("Web results for " + searchTerms);
System.out.println("Displaying " + (offset+1) + " to " + (offset + results.getLength()) + " of " + total + " results ");
System.out.println();
// Display the Web results.
StringBuilder builder = new StringBuilder();
for(int i = 0 ; i < results.getLength(); i++)
{
NodeList childNodes = results.item(i).getChildNodes();
for (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();
for (int i = 0; i < errors.getLength(); i++)
{
NodeList childNodes = errors.item(i).getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++)
{
System.out.println(childNodes.item(j).getLocalName() + ":" + childNodes.item(j).getTextContent());
}
System.out.println();
}
}
}
QUESTION : I want to get all the results from a specific domain. How to do that ?
and Should I download any libraries ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在字符串查询中,您给出
的应该可以工作,它将单独返回来自 www.apple.com 的结果。
In String query you give
It should work, It will return the result from www.apple.com alone.