1,引言

本文讲解怎样用Java使用 GooSeeker API 接口下载内容提取器,这是一个示例程序。什么是内容提取器?为什么用这种方式?源自GooSeeker的Python开源项目:通过生成内容提取器,大幅节省程序员时间。具体请参看《内容提取器的定义》。

这是一系列实例程序中的一个,就目前编程语言发展来看,Java实现网页内容提取并不合适,除了语言不够灵活便捷以外,整个生态不够活跃,可选的类库增长缓慢。另外,要从JavaScript动态网页中提取内容,Java也很不方便,需要一个JavaScript引擎。其它更合适的编程语言示例是:

2,具体实现

注解:
  • 使用java类库 jsoup(1.8.3以上版本),可以很便利、快速的获取网页dom。
  • 通过GooSeeker API 获取xslt(参考 1分钟快速生成用于网页内容提取的xslt
  • 使用java自带的类TransformerFactory执行网页内容转换

源代码如下:

  1.     public static void main(String[] args)
  2.     {
  3.         InputStream xslt = null;
  4.         try
  5.         {
  6.             String grabUrl = "http://m.58.com/cs/qiuzu/22613961050143x.shtml"; // 抓取网址
  7.             String resultPath = "F:/temp/xslt/result.xml"; // 抓取结果文件的存放路径
  8.             // 通过GooSeeker API接口获得xslt
  9.             xslt = getGsExtractor();
  10.             // 抓取网页内容转换结果文件
  11.             convertXml(grabUrl, xslt, resultPath);
  12.         } catch (Exception e)
  13.         {
  14.             e.printStackTrace();
  15.         } finally
  16.         {
  17.             try
  18.             {
  19.                 if (xslt != null)
  20.                     xslt.close();
  21.             } catch (IOException e)
  22.             {
  23.                 e.printStackTrace();
  24.             }
  25.         }
  26.     }

  27.     /**
  28.      * @description dom转换
  29.      */
  30.     public static void convertXml(String grabUrl, InputStream xslt, String resultPath) throws Exception
  31.     {
  32.         // 这里的doc对象指的是jsoup里的Document对象
  33.         org.jsoup.nodes.Document doc = Jsoup.parse(new URL(grabUrl).openStream(), "UTF-8", grabUrl);
  34.         W3CDom w3cDom = new W3CDom();
  35.         // 这里的w3cDoc对象指的是w3c里的Document对象
  36.         org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(doc);
  37.         Source srcSource = new DOMSource(w3cDoc);
  38.         TransformerFactory tFactory = TransformerFactory.newInstance();
  39.         Transformer transformer = tFactory.newTransformer(new StreamSource(xslt));
  40.         transformer.transform(srcSource, new StreamResult(new FileOutputStream(resultPath)));
  41.     }

  42.     /**
  43.      * @description 获取API返回结果
  44.      */
  45.     public static InputStream getGsExtractor()
  46.     {
  47.         // api接口
  48.         String apiUrl = "http://www.gooseeker.com/api/getextractor";
  49.         // 请求参数
  50.         Map<String,Object> params = new HashMap<String, Object>();
  51.         params.put("key", "xxx");  // Gooseeker会员中心申请的API KEY
  52.         params.put("theme", "xxx");  // 提取器名,就是用MS谋数台定义的规则名
  53.         params.put("middle", "xxx");  // 规则编号,如果相同规则名下定义了多个规则,需填写
  54.         params.put("bname", "xxx"); // 整理箱名,如果规则含有多个整理箱,需填写
  55.         String httpArg = urlparam(params);
  56.         apiUrl = apiUrl + "?" + httpArg;
  57.         InputStream is = null;
  58.         try
  59.         {
  60.             URL url = new URL(apiUrl);
  61.             HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
  62.             urlCon.setRequestMethod("GET");
  63.             is = urlCon.getInputStream();
  64.         } catch (ProtocolException e)
  65.         {
  66.             e.printStackTrace();
  67.         } catch (IOException e)
  68.         {
  69.             e.printStackTrace();
  70.         }
  71.         return is;
  72.     }

  73.     /**
  74.      * @description 请求参数
  75.      */
  76.     public static String urlparam(Map<String, Object> data)
  77.     {
  78.         StringBuilder sb = new StringBuilder();
  79.         for (Map.Entry<String, Object> entry : data.entrySet())
  80.         {
  81.             try
  82.             {
  83.                 sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue() + "", "UTF-8")).append("&");
  84.             } catch (UnsupportedEncodingException e)
  85.             {
  86.                 e.printStackTrace();
  87.             }
  88.         }
  89.         return sb.toString();
  90.     }
复制代码
返回结果如下:
3,展望
同样可以用python来获取指定网页内容,感觉python的语法更加简洁,后续增加python语言的示例,有兴趣的小伙伴可以加入一起研究

4,相关文档
1, GooSeeker API说明(1)--下载内容提取器
2, GooSeeker API例子——用JavaScript下载内容提取器

5,集搜客GooSeeker开源代码下载源
1, GooSeeker开源Python网络爬虫GitHub源

6,文档修改历史
1,
2016-06-07:V1.0
2,2016-06-10:V2.0,将JavaScript代码分出去另起一篇






举报 使用道具
| 回复

共 0 个关于本帖的回复 最后回复于 2016-6-7 18:49

您需要登录后才可以回帖 登录 | 立即注册

精彩推荐

  • Gephi社会网络分析-马蜂窝游记文本分词并同
  • Gephi社会网络分析-基于马蜂窝游记文本以词
  • 知乎话题文本根据词语间距筛选后生成共词矩
  • 马蜂窝游记文本分词后以词语间距为筛选条件
  • 学习使用apriori算法挖掘关联关系

热门用户

GMT+8, 2024-4-19 21:16