search-demo演示了如何利用Java来调用百度搜索和谷歌搜索,更多细节请到github上查看search-demo
自己没搜索引擎,又想要大规模的数据源,怎么办?可以对百度搜索和谷歌搜索善加利用,以小搏大,站在巨人的肩膀上。有很多的应用场景可以很巧妙地借助百度搜索和谷歌搜索来实现,比如网站的新闻采集,比如技术、品牌的新闻跟踪,比如知识库的收集,比如人机问答系统等,我之前做的一个准确率达百分之九十几的人机问答系统的数据源,其中一部分就是充分利用了百度搜索和谷歌搜索。在此演示的技术的基础上,可以容易地扩展到其他的搜索引擎,可以借鉴使用的NekoHTML+XPath或JSoup+CSSPath技术,轻松获取页面的自定义的内容。
实现方式一:NekoHTML+XPath
- package org.apdplat.demo.search;
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.cyberneko.html.parsers.DOMParser;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- import com.sun.org.apache.xpath.internal.XPathAPI;
- import javax.xml.transform.TransformerException;
- import org.w3c.dom.DOMException;
- import org.xml.sax.SAXException;
- public class NekoHTMLBaiduSearcher implements Searcher{
- private static final Logger LOG = LoggerFactory.getLogger(NekoHTMLBaiduSearcher.class);
- public List<String> parse(String url, String xpathExpression) {
- InputStream in = null;
- try {
- in = new URL(url).openStream();
- return parse(in, xpathExpression);
- } catch (Exception e) {
- LOG.error("错误", e);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- LOG.error("错误", e);
- }
- }
- }
- return null;
- }
- public List<String> parse(InputStream in, String xpathExpression) {
- return parse(in, xpathExpression, "UTF-8");
- }
- public List<Map<String, String>> parseMore(InputStream in, String xpathExpression) {
- return parseMore(in, xpathExpression, "UTF-8");
- }
- public List<Map<String, String>> parseMore(InputStream in, String xpathExpression, String encoding) {
- DOMParser parser = new DOMParser();
- List<Map<String, String>> list = new ArrayList<>();
- try {
- // 设置网页的默认编码
- parser.setProperty(
- "http://cyberneko.org/html/properties/default-encoding",
- encoding);
- /*
- * The Xerces HTML DOM implementation does not support namespaces
- * and cannot represent XHTML documents with namespace information.
- * Therefore, in order to use the default HTML DOM implementation
- * with NekoHTML's DOMParser to parse XHTML documents, you must turn
- * off namespace processing.
- */
- parser.setFeature("http://xml.org/sax/features/namespaces", false);
- parser.parse(new InputSource(new BufferedReader(new InputStreamReader(in, encoding))));
- Document doc = parser.getDocument();
- NodeList products = XPathAPI.selectNodeList(doc, xpathExpression.toUpperCase());
- for (int i = 0; i < products.getLength(); i++) {
- Node node = products.item(i);
- String title = node.getTextContent();
- Map<String, String> map = new HashMap<>();
- map.put("title", title);
- try {
- String href = node.getAttributes().getNamedItem("href").getTextContent();
- map.put("href", href);
- } catch (Exception e) {
- LOG.error("提取链接失败",e);
- }
- list.add(map);
- }
- } catch (SAXException | IOException | TransformerException | DOMException e) {
- LOG.error("错误", e);
- }
- return list;
- }
- public List<String> parse(InputStream in, String xpathExpression, String encoding) {
- DOMParser parser = new DOMParser();
- List<String> list = new ArrayList<>();
- try {
- // 设置网页的默认编码
- parser.setProperty(
- "http://cyberneko.org/html/properties/default-encoding",
- encoding);
- /*
- * The Xerces HTML DOM implementation does not support namespaces
- * and cannot represent XHTML documents with namespace information.
- * Therefore, in order to use the default HTML DOM implementation
- * with NekoHTML's DOMParser to parse XHTML documents, you must turn
- * off namespace processing.
- */
- parser.setFeature("http://xml.org/sax/features/namespaces", false);
- parser.parse(new InputSource(new BufferedReader(new InputStreamReader(in, encoding))));
- Document doc = parser.getDocument();
- NodeList products = XPathAPI.selectNodeList(doc, xpathExpression.toUpperCase());
- for (int i = 0; i < products.getLength(); i++) {
- Node node = products.item(i);
- list.add(node.getTextContent());
- }
- } catch (SAXException | IOException | TransformerException | DOMException e) {
- LOG.error("错误", e);
- }
- return list;
- }
- @Override
- public List<Webpage> search(String url) {
- InputStream in = null;
- try {
- in = new URL(url).openStream();
- return search(in);
- } catch (Exception e) {
- LOG.error("错误", e);
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- LOG.error("错误", e);
- }
- }
- }
- return null;
- }
- public List<Webpage> search(InputStream in) {
- //保证只读一次
- byte[] datas = Tools.readAll(in);
- if (LOG.isDebugEnabled()) {
- try {
- LOG.debug("内容:" + new String(datas, "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- LOG.error("错误", e);
- }
- }
- in = new ByteArrayInputStream(datas);
- String totalXpathExpression = "//html/body/div/div/div/div[3]/p/span";
- List<String> totals = parse(in, totalXpathExpression);
- int total;
- int len = 10;
- if (totals != null && totals.size() == 1) {
- String str = totals.get(0);
- int start = 10;
- if (str.indexOf("约") != -1) {
- start = 11;
- }
- total = Integer.parseInt(str.substring(start).replace(",", "").replace("个", ""));
- LOG.info("搜索结果数:" + total);
- } else {
- return null;
- }
- if (total < 1) {
- return null;
- }
- if (total < 10) {
- len = total;
- }
- List<Webpage> webpages = new ArrayList<>();
- for (int i = 0; i < len; i++) {
- String content = "";
- String url = "";
- String titleXpathExpression = "//html/body/div/div/div/div[3]/div[2]/table[" + (i + 1) + "]/tbody/tr/td/h3/a";
- String summaryXpathExpression = "//html/body/div/div/div/div[3]/div[2]/table[" + (i + 1) + "]/tbody/tr/td/div[1]";
- LOG.debug("titleXpathExpression:" + titleXpathExpression);
- LOG.debug("summaryXpathExpression:" + summaryXpathExpression);
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<String> titles = parse(in, titleXpathExpression);
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<Map<String, String>> titleWithHrefs = parseMore(in, titleXpathExpression);
- for (Map<String, String> titleWithHref : titleWithHrefs) {
- String title = titleWithHref.get("title");
- String href = titleWithHref.get("href");
- LOG.debug(title + " " + titleWithHref.get("href"));
- if (href != null) {
- content = Tools.getHTMLContent(href);
- url = href;
- } else {
- LOG.info("页面正确提取失败");
- }
- }
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<String> summaries = parse(in, summaryXpathExpression);
- //处理百度知道1
- if (titles != null && titles.size() == 1 && (summaries == null || summaries.isEmpty())) {
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- String baiduZhidao1XpathExpression = "//html/body/div/div/div/div[3]/div[2]/table[" + (i + 1) + "]/tbody/tr/td/font[2]/div/div/p[2]";
- LOG.debug("baiduZhidao1XpathExpression:" + baiduZhidao1XpathExpression);
- summaries = parse(in, baiduZhidao1XpathExpression);
- }
- //处理百度知道2
- if (titles != null && titles.size() == 1 && (summaries == null || summaries.isEmpty())) {
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- String baiduZhidao2XpathExpression = "//html/body/div/div/div/div[3]/div[2]/table[" + (i + 1) + "]/tbody/tr/td/font[2]";
- LOG.debug("baiduZhidao2XpathExpression:" + baiduZhidao2XpathExpression);
- summaries = parse(in, baiduZhidao2XpathExpression);
- }
- //处理百度文库
- if (titles != null && titles.size() == 1 && (summaries == null || summaries.isEmpty())) {
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- String baiduWenkuXpathExpression = "//html/body/div/div/div/div[3]/div[2]/table[" + (i + 1) + "]/tbody/tr/td/font[1]";
- LOG.debug("baiduWenkuXpathExpression:" + baiduWenkuXpathExpression);
- summaries = parse(in, baiduWenkuXpathExpression);
- }
- if (titles != null && titles.size() == 1 && summaries != null && summaries.size() == 1) {
- Webpage webpage = new Webpage();
- webpage.setTitle(titles.get(0));
- webpage.setUrl(url);
- webpage.setSummary(summaries.get(0));
- webpage.setContent(content);
- webpages.add(webpage);
- } else {
- LOG.error("获取搜索结果列表项出错:" + titles + " - " + summaries);
- }
- }
- if(webpages.size() < 10){
- //处理百度百科
- String titleXpathExpression = "//html/body/div/div/div/div[3]/div[2]/div/h3/a";
- String summaryXpathExpression = "//html/body/div/div/div/div[3]/div[2]/div/div/p";
- LOG.debug("处理百度百科 titleXpathExpression:" + titleXpathExpression);
- LOG.debug("处理百度百科 summaryXpathExpression:" + summaryXpathExpression);
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<String> titles = parse(in, titleXpathExpression);
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<Map<String, String>> titleWithHrefs = parseMore(in, titleXpathExpression);
- String content = "";
- String url = "";
- for (Map<String, String> titleWithHref : titleWithHrefs) {
- String title = titleWithHref.get("title");
- String href = titleWithHref.get("href");
- LOG.debug(title + " " + titleWithHref.get("href"));
- if (href != null) {
- content = Tools.getHTMLContent(href);
- url = href;
- } else {
- LOG.info("页面正确提取失败");
- }
- }
- //重新构造输入流
- in = new ByteArrayInputStream(datas);
- List<String> summaries = parse(in, summaryXpathExpression);
- if (titles != null && titles.size() == 1 && summaries != null && summaries.size() == 1) {
- Webpage webpage = new Webpage();
- webpage.setTitle(titles.get(0));
- webpage.setUrl(url);
- webpage.setSummary(summaries.get(0));
- webpage.setContent(content);
- webpages.add(webpage);
- } else {
- LOG.error("获取搜索结果列表项出错:" + titles + " - " + summaries);
- }
- }
- if (webpages.isEmpty()) {
- return null;
- }
- return webpages;
- }
- public static void main(String[] args) {
- String url = "http://www.baidu.com/s?pn=0&wd=杨尚川";
- Searcher searcher = new NekoHTMLBaiduSearcher();
- List<Webpage> webpages = searcher.search(url);
- if (webpages != null) {
- int i = 1;
- for (Webpage webpage : webpages) {
- LOG.info("搜索结果 " + (i++) + " :");
- LOG.info("标题:" + webpage.getTitle());
- LOG.info("URL:" + webpage.getUrl());
- LOG.info("摘要:" + webpage.getSummary());
- LOG.info("正文:" + webpage.getContent());
- LOG.info("");
- }
- } else {
- LOG.error("没有搜索到结果");
- }
- }
- }
实现方式二:JSoup+CSSPath
- package org.apdplat.demo.search;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class JSoupBaiduSearcher implements Searcher{
- private static final Logger LOG = LoggerFactory.getLogger(JSoupBaiduSearcher.class);
- @Override
- public List<Webpage> search(String url) {
- List<Webpage> webpages = new ArrayList<>();
- try {
- Document document = Jsoup.connect(url).get();
- String cssQuery = "html body div#out div#in div#wrapper div#container.container_s p#page span.nums";
- LOG.debug("total cssQuery: " + cssQuery);
- Element totalElement = document.select(cssQuery).first();
- String totalText = totalElement.text();
- LOG.info("搜索结果:" + totalText);
- int start = 10;
- if (totalText.indexOf("约") != -1) {
- start = 11;
- }
- int total = Integer.parseInt(totalText.substring(start).replace(",", "").replace("个", ""));
- LOG.info("搜索结果数:" + total);
- int len = 10;
- if (total < 1) {
- return null;
- }
- if (total < 10) {
- len = total;
- }
- for (int i = 0; i < len; i++) {
- String titleCssQuery = "html body div#out div#in div#wrapper div#container.container_s div#content_left table#" + (i + 1) + ".result tbody tr td.c-default h3.t a";
- String summaryCssQuery = "html body div#out div#in div#wrapper div#container.container_s div#content_left table#" + (i + 1) + ".result tbody tr td.c-default div.c-abstract";
- LOG.debug("titleCssQuery:" + titleCssQuery);
- LOG.debug("summaryCssQuery:" + summaryCssQuery);
- Element titleElement = document.select(titleCssQuery).first();
- String href = "";
- String titleText = "";
- if(titleElement != null){
- titleText = titleElement.text();
- href = titleElement.attr("href");
- }else{
- //处理百度百科
- titleCssQuery = "html body div#out div#in div#wrapper div#container.container_s div#content_left div#1.result-op h3.t a";
- summaryCssQuery = "html body div#out div#in div#wrapper div#container.container_s div#content_left div#1.result-op div p";
- LOG.debug("处理百度百科 titleCssQuery:" + titleCssQuery);
- LOG.debug("处理百度百科 summaryCssQuery:" + summaryCssQuery);
- titleElement = document.select(titleCssQuery).first();
- if(titleElement != null){
- titleText = titleElement.text();
- href = titleElement.attr("href");
- }
- }
- LOG.debug(titleText);
- Element summaryElement = document.select(summaryCssQuery).first();
- //处理百度知道
- if(summaryElement == null){
- summaryCssQuery = summaryCssQuery.replace("div.c-abstract","font");
- LOG.debug("处理百度知道 summaryCssQuery:" + summaryCssQuery);
- summaryElement = document.select(summaryCssQuery).first();
- }
- String summaryText = "";
- if(summaryElement != null){
- summaryText = summaryElement.text();
- }
- LOG.debug(summaryText);
- if (titleText != null && !"".equals(titleText.trim()) && summaryText != null && !"".equals(summaryText.trim())) {
- Webpage webpage = new Webpage();
- webpage.setTitle(titleText);
- webpage.setUrl(href);
- webpage.setSummary(summaryText);
- if (href != null) {
- String content = Tools.getHTMLContent(href);
- webpage.setContent(content);
- } else {
- LOG.info("页面正确提取失败");
- }
- webpages.add(webpage);
- } else {
- LOG.error("获取搜索结果列表项出错:" + titleText + " - " + summaryText);
- }
- }
- } catch (IOException ex) {
- LOG.error("搜索出错",ex);
- }
- return webpages;
- }
- public static void main(String[] args) {
- String url = "http://www.baidu.com/s?pn=0&wd=杨尚川";
- Searcher searcher = new JSoupBaiduSearcher();
- List<Webpage> webpages = searcher.search(url);
- if (webpages != null) {
- int i = 1;
- for (Webpage webpage : webpages) {
- LOG.info("搜索结果 " + (i++) + " :");
- LOG.info("标题:" + webpage.getTitle());
- LOG.info("URL:" + webpage.getUrl());
- LOG.info("摘要:" + webpage.getSummary());
- LOG.info("正文:" + webpage.getContent());
- LOG.info("");
- }
- } else {
- LOG.error("没有搜索到结果");
- }
- }
- }
相关推荐
百度搜索接口
使用前,导入lib文件夹下的包,点击运行就可以了。而且可以利用此程序设计SO-PMI算法的实现。获得百度搜索数的同时得到两个词语的极性
Java EE调用百度API,进行人脸注册,注册后可以登陆,会调用百度API人脸搜索,搜索成功即可登陆,填写自己申请的百度API密钥即可使用
百度输入联想!使用jsonp调用百度的js实现搜索关键字的智能提示,拿来可以直接使用!
【Java毕业设计】代毕业设计,java调用百度地图API实现西安旅游景点搜索、路线规划导航
本人原创测试了百度,搜狗,360搜索都可以获取重定向后的真实地址,直接运行就可以,也可以根据自己项目具体修改功能。java使用HttpURLConnection获取百度搜狗360搜索链接重定向真实目标链接地址url。
利用元搜索下在百度 doc 文件。示例 利用httpclient 模拟像百度发请求
小弟用java写的一个桌面应用程序,可以读取百度新闻热搜词的前10条,呵呵,大家支持下啦
Java多线程大量调用百度地图的搜索功能,来做课程作业数据准备 读取xls 采用了apache.poi,说实话,使用体验上来说肯定没有python的pandas还用, 多线程共享资源 主要共享的就是一个sheet对象和访问列标row_number...
实现百度地图动态搜索、静态地图动态插入 mygw@163.com js 内容如下: /** 加载地图的主控类 */ var MapControl={ staticWith : 512,//链接静态图宽度 staticHeight : 320,//链接静态图的高度 ...
Java EE调用百度API,进行人脸注册,注册后可以登陆,会调用百度API人脸搜索,搜索成功即可登陆,
基于百度AI使用H5实现调用摄像头进行人脸注册、人脸搜索功能(Java) 前端使用trackingjs
基于百度AI使用H5实现调用摄像头进行人脸注册、人脸搜索功能(Java),前端使用trackingjs
主要介绍了Java调用浏览器打开网页的方法,以完整实例形式分析了java打开网页的相关技巧,需要的朋友可以参考下
非常详细的程序,可以用来干很多事情。里面的程序可以直接在Eclpise上运行,不用加其他的jar包。
百度云盘分享 简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对...
这个 Java 包可以使用抓取和解析 Google、Bing 和百度搜索结果。 随意 fork 这个存储库以添加更多后端。 该项目是 Java 7 中 SerpApi 的实现。此代码依赖 GSON 进行高效的 JSON 处理。 HTTP 响应被转换为 JSON ...
配置文件英文版需要翻译成中文版,总共快两万行了,但是翻译软件总会有字符限制,例如百度翻译5000个字符限制,bing翻译,最多的是google翻译,但是还是有字符限制,所以想到调用Translator API来自动翻译,使用的是...
post调用接口(附带身份验证和自带参数的方式),自己在百度上搜索了很多都不如意,最后自己整理了一套代码示例