zoukankan      html  css  js  c++  java
  • HttpClient+Jsoup 抓取网页信息(网易贵金属为例)

    废话不多说直接讲讲今天要做的事。

    利用HttpClient和Jsoup技术抓取网页信息。HttpClient是支持HTTP协议的客户端编程工具包,并且它支持HTTP协议。

    jsoup 是一款基于 Java 平台的 网页html解析器,可直接解析某个 URL 地址、HTML 文本内容,提供了一套非常方便的 API接口,通过类似于 jQuery 的操作方法来操作数据。

    httpClient相关文档:http://hc.apache.org/httpcomponents-client-5.0.x/index.html

    jsoup相关文档:http://jsoup.org/

    此处以网易贵金属资讯为例进行案例教学 O(∩_∩)O

    然后我们首先要分析网页源代码的结构

    之后我们就可以开始进行编程了,首先我们要知道利用httpClient的流程:

      1. 创建HttpClient的对象;

      2. 创建请求方法的实例,并指定访问的URL;

      3. 调用HttpClient对象发送请求,该方法返回一个HttpResponse,要判断responce.getStatusLine().getStatusCode()的返回码是否为200;

      4. 调用HttpResponse相关方法获取相应内容;

      5. 释放连接。

    当然啦 创建项目的时候要导入相关的jar包,本文会提供源码+jar包http://pan.baidu.com/s/1sl55d85

     StockUtils.java

     1 package cn.clay.httpclient.utils;
     2 
     3 import java.io.IOException;
     4 
     5 import org.apache.http.HttpEntity;
     6 import org.apache.http.HttpResponse;
     7 import org.apache.http.HttpStatus;
     8 import org.apache.http.client.HttpClient;
     9 import org.apache.http.client.methods.HttpGet;
    10 import org.apache.http.impl.client.CloseableHttpClient;
    11 
    12 import org.apache.http.impl.client.HttpClients;
    13 import org.apache.http.util.EntityUtils;
    14 /**
    15  * 传递网页链接
    16  * 返回网页源码
    17  * @author ClayZhang
    18  *
    19  */
    20 public class StockUtils {
    21     //第一次获取网页源码
    22     public static String getHtmlByUrl(String url) throws IOException{  
    23         String html = null;  
    24         CloseableHttpClient httpClient = HttpClients.createDefault();//创建httpClient对象   
    25         HttpGet httpget = new HttpGet(url);
    26         try {  
    27             HttpResponse responce = httpClient.execute(httpget);
    28             int resStatu = responce.getStatusLine().getStatusCode();
    29             if (resStatu == HttpStatus.SC_OK) {
    30                   
    31                 HttpEntity entity = responce.getEntity();  
    32                 if (entity != null) {  
    33                     html = EntityUtils.toString(entity);//获得html源代码
    34                 }  
    35             }  
    36         } catch (Exception e) {
    37             System.out.println("访问【"+url+"】出现异常!");  
    38             e.printStackTrace();  
    39         } finally {
    40             //释放连接
    41             httpClient.close();  
    42         }  
    43         return html;  
    44     }  
    45 }

    然后利用jsoup的方法进行测试类的编写StockTest.java

     1 package cn.clay.httpclient.utils.test;
     2 
     3 import java.io.IOException;
     4 
     5 import org.apache.http.ParseException;
     6 import org.jsoup.Jsoup;
     7 import org.jsoup.nodes.Document;
     8 import org.jsoup.nodes.Element;
     9 import org.jsoup.select.Elements;
    10 
    11 import cn.clay.httpclient.utils.StockUtils;
    12 
    13 /**
    14  * 
    15  * @author ClayZhang
    16  *
    17  */
    18 public class StockTest {
    19 
    20     public static void main(String[] args) throws ParseException, IOException {
    21         String content = StockUtils.getHtmlByUrl(
    22                 "http://fa.163.com/zx/gjs/1/");
    23         parserHtml(content);
    24     }
    25 
    26     
    27     public static void parserHtml(String content) throws ParseException, IOException {
    28         Document doc = Jsoup.parse(content);
    29         Elements links = doc.getElementsByClass("g-news").select("dl");
    30         for (Element e : links) {
    31             System.out.println("新闻标题:" + e.select("a").text().toString());
    32             //获取页面链接
    33             Elements linkHref = e.select("a");
    34             //截取时间字符串
    35             Elements timeStr = e.select("span[class=f-fr]");
    36             //简略信息
    37             Elements comment = e.select("span[class=f-fl f-ofe u-digest]");
    38             System.out.println("新闻链接:" + linkHref.attr("href"));
    39             System.out.println("发布时间:" + timeStr.text());
    40             System.out.println("简要信息:" + comment.text().toString());
    41             
    42             System.out.println("=============================================================");
    43         }
    44         
    45     }
    46 }

    运行之后的效果如下

    本文版权归作者及博客园所有,转载请注明作者及原文出处

    http://www.cnblogs.com/clayzhang

  • 相关阅读:
    1062 Talent and Virtue (25 分)
    1083 List Grades (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1121 Damn Single (25 分)
    1120 Friend Numbers (20 分)
    1084 Broken Keyboard (20 分)
    1092 To Buy or Not to Buy (20 分)
    数组与链表
    二叉树
    时间复杂度与空间复杂度
  • 原文地址:https://www.cnblogs.com/clayzhang/p/6707872.html
Copyright © 2011-2022 走看看