爬虫小demo

运用apache httpclient爬数据、httpcleaner解析爬回来的数据:

package cn.sniper.spider.util;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
public class SpiderUtil {
 
 private String pageContent;
 
 @Before
 public void init() {
  HttpClientBuilder builder = HttpClients.custom();
  CloseableHttpClient client = builder.build();
  String url = "http://www.2345.com/";
  
  HttpGet request = new HttpGet(url);
  
  try {
   CloseableHttpResponse resp = client.execute(request);
   HttpEntity entity = resp.getEntity();
   pageContent = EntityUtils.toString(entity);
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 /**
  * 抓取整个页面
  */
 @Test
 public void testDownload1() {
  HttpClientBuilder builder = HttpClients.custom();
  CloseableHttpClient client = builder.build();
  String url = "http://www.2345.com/";
  
  HttpGet request = new HttpGet(url);
  
  try {
   CloseableHttpResponse resp = client.execute(request);
   HttpEntity entity = resp.getEntity();
   String pageContent = EntityUtils.toString(entity);
   System.out.println(pageContent);
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 /**
  * 取得text内容
  */
 @Test
 public void testDownload2() {
  HtmlCleaner cleaner = new HtmlCleaner();
  TagNode rootNode = cleaner.clean(pageContent);
  
  //拿到id=name元素中的第一个h1元素,如果只有一个,//*[@id=\"name\"]h1
  String xPathExpression = "//*[@id=\"name\"]h1[1]";
  
  try {
   Object[] objs = rootNode.evaluateXPath(xPathExpression);
   
   TagNode node = (TagNode)objs[0];
   System.out.println(node.getText());
  } catch (XPatherException e) {
   e.printStackTrace();
  }
  
 }
 
 /**
  * 通过属性名称取得值
  */
 @Test
 public void testDownload3() {
  HtmlCleaner cleaner = new HtmlCleaner();
  TagNode rootNode = cleaner.clean(pageContent);
  
  String xPathExpression = "//*[@id=\"j_search_img\"]";
  
  try {
   Object[] objs = rootNode.evaluateXPath(xPathExpression);
   
   TagNode node = (TagNode)objs[0];
   String src = node.getAttributeByName("src");
   
   //注意,需要写前缀:http:// 否则报错:java.net.MalformedURLException: no protocol
   URL url = new URL("http://www.2345.com/" + src);
   URLConnection conn = url.openConnection();
   InputStream is = conn.getInputStream();
   
   FileOutputStream fos = new FileOutputStream("D:/1.gif");
   
   int b = 0;
   while((b = is.read()) != -1) {
    fos.write(b);
   }
   
   fos.close();
   is.close();
   
   System.out.println(src);
  } catch (XPatherException e) {
   e.printStackTrace();
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
 /**
  * 抓取的页面返回json数据
  */
 @Test
 public void testDownload4() {
  HttpClientBuilder builder = HttpClients.custom();
  CloseableHttpClient client = builder.build();
  String url = "http://www.2345.com/";
  
  HttpGet request = new HttpGet(url);
  
  try {
   CloseableHttpResponse resp = client.execute(request);
   HttpEntity entity = resp.getEntity();
   String pageContent = EntityUtils.toString(entity);
   
   JSONArray jsonArray = new JSONArray(pageContent);
   JSONObject jsonObj = (JSONObject)jsonArray.get(0);
   System.out.println(jsonObj.get("price"));
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
}


转载于:https://my.oschina.net/sniperLi/blog/493786

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值