SAE提供了一个高效的网页分布式抓取的服务,可与其他网页分析的程序一同使用。
本文仍是用JAVA实现,使用前要导入sae-local-1.1.0.jar,官网有提供下载。
相关的类为 com.sina.sae.fetchurl.SaeFetchurl
示例代码如下
//发起POST请求
SaeFetchurl fetchUrl = new SaeFetchurl();
fetchUrl.setMethod("post");//请求方式
Map<String,String> maps = new HashMap<String, String>();
maps.put("name1","value1");
maps.put("name2","value2");
fetchUrl.setPostData(maps);//设置请求参数
String result = fetchUrl.fetch("http://softwarehouse.sinaapp.com/");//设置需要抓取的URL
respContent += result;
//抓取失败时输出错误码和错误信息
System.out.println(fetchUrl.getErrno());
System.out.println(fetchUrl.getErrmsg());
如果是需要登录的网页,则需要添加如下代码:
fetchUrl.setHttpAuth(username, password);
更多使用方法,请查看官方文档 http://sae4java.sinaapp.com/doc/com/sina/sae/fetchurl/SaeFetchurl.html