爬虫之HttpClient与Jsoup

HttpClient:

(1)实现了所有 HTTP 的方法(GET,POST,PUT,DELETE 等)
(2)支持自动转向
(3)支持 HTTPS 协议
(4)支持代理服务器等

//pom.xml
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
//基本用法
public class HttpClientDemo {

    public static void main(String[] args) throws Exception {
        String url = "http://www.baidu.com";
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // POST请求
        // 创建一个post对象
        HttpPost post = new HttpPost(url);
        // 添加头信息
        post.addHeader("head1", "123");
        post.setHeader("Cookie", "aaa");
        // 添加参数
        List<NameValuePair> kvList = new ArrayList<>();
        kvList.add(new BasicNameValuePair("param1", "111"));
        kvList.add(new BasicNameValuePair("param2", "222"));
        // 包装成一个Entity对象
        StringEntity entity = new UrlEncodedFormEntity(kvList, "UTF8");
        // 设置请求的内容
        post.setEntity(entity);
        // 执行post请求
        CloseableHttpResponse response = httpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(), "UTF8");
        System.out.println(result);

        // GET请求
        // 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
        URIBuilder uriBuilder = new URIBuilder(url);
        List<NameValuePair> list = new LinkedList<>();
        list.add(new BasicNameValuePair("h1", "123"));
        uriBuilder.setParameters(list);
        // 创建Get对象
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        // 执行Get请求
        CloseableHttpResponse resp = httpClient.execute(httpGet);
        String ret = EntityUtils.toString(resp.getEntity(), "UTF8");
        System.out.println(ret);

        resp.close();
        httpClient.close();
    }
}
//工具类
public class HttpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static SSLConnectionSocketFactory sslsf = null;
    private static PoolingHttpClientConnectionManager cm = null;
    private static SSLContextBuilder builder = null;
    static {
        try {
            builder = new SSLContextBuilder();
            // 全部信任 不做身份鉴定
            builder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            });
            sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register(HTTP, new PlainConnectionSocketFactory())
                    .register(HTTPS, sslsf)
                    .build();
            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);//max connection
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String doPost(String url, Map<String,String> heads, Map<String,String> params){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建一个post对象
            HttpPost post =new HttpPost(url);
            if(heads != null){
                Set<Entry<String, String>> entrySet = heads.entrySet();
                for(Entry<String, String> entry : entrySet){
                    post.addHeader(entry.getKey(), entry.getValue());
                }
            }
            if(params != null){
                Set<Entry<String, String>> entrySet = params.entrySet();
                List<NameValuePair>kvList = new ArrayList<>();
                for(Entry<String, String> entry : entrySet){
                    kvList.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
                }
                //包装成一个Entity对象
                StringEntity entity = new UrlEncodedFormEntity(kvList,"UTF8");
                //设置请求的内容
                post.setEntity(entity);
                
            }
            //执行post请求
            CloseableHttpResponse response = httpClient.execute(post);
            String result = EntityUtils.toString(response.getEntity(),"UTF8");
            response.close();
            httpClient.close();
            return result;
        } catch (ClientProtocolException e) {
            logger.error("post请求异常:{}",e);
        } catch (ParseException e) {
            logger.error("post请求异常:{}",e);
        } catch (IOException e) {
            logger.error("post请求异常:{}",e);
        }
        return null;
    }
    
    public static String doPostWithEntity(String url, JSONObject params){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建一个post对象
            HttpPost post =new HttpPost(url);
            post.addHeader("Content-Type", "application/json");
            if(params != null){
                post.setEntity(new StringEntity(params.toJSONString(),"UTF8"));
            }
            //执行post请求
            CloseableHttpResponse response = httpClient.execute(post);
            String result = EntityUtils.toString(response.getEntity(),"UTF8");
            response.close();
            httpClient.close();
            return result;
        } catch (ClientProtocolException e) {
            logger.error("post请求异常:{}",e);
        } catch (ParseException e) {
            logger.error("post请求异常:{}",e);
        } catch (IOException e) {
            logger.error("post请求异常:{}",e);
        }
        return null;
    }
    
    public static byte[] doPostToStream(String url, JSONObject params){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建一个post对象
            HttpPost post =new HttpPost(url);
            post.addHeader("Content-Type", "application/json");
            if(params != null){
                post.setEntity(new StringEntity(params.toJSONString(),"UTF8"));
            }
            //执行post请求
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity httpEntity = response.getEntity();
            ContentType contentType = ContentType.getOrDefault(httpEntity);
            if(ContentType.IMAGE_JPEG.getMimeType().equals(contentType.getMimeType())){
                byte[] array = EntityUtils.toByteArray(httpEntity);
                return array;
            }else{
                String result = EntityUtils.toString(response.getEntity(),"UTF8");
                logger.warn("doPostToStream方法请求失败:{}",result);
            }
            response.close();
            httpClient.close();
        } catch (ClientProtocolException e) {
            logger.error("post请求异常:{}",e);
        } catch (ParseException e) {
            logger.error("post请求异常:{}",e);
        } catch (IOException e) {
            logger.error("post请求异常:{}",e);
        }
        return null;
    }
    
    public static String doGetWithHttps(String url, Map<String,String> heads, Map<String,String> params){
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .setConnectionManager(cm)
                    .setConnectionManagerShared(true)
                    .build();
            /*
             * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
             */
             URIBuilder uriBuilder = new URIBuilder(url);
             if(params != null){
                 Set<Entry<String, String>> entrySet = params.entrySet();
                 List<NameValuePair> list = new LinkedList<>();
                 for(Entry<String, String> entry : entrySet){
                     list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
                 }
                 uriBuilder.setParameters(list);

             }

             // 根据带参数的URI对象构建GET请求对象
             HttpGet httpGet = new HttpGet(uriBuilder.build());

             if(heads != null){
                 Set<Entry<String, String>> entrySet = heads.entrySet();
                 for(Entry<String, String> entry : entrySet){
                     httpGet.addHeader(entry.getKey(), entry.getValue());
                 }
             }

             //执行post请求
             CloseableHttpResponse response = httpClient.execute(httpGet);
             result = EntityUtils.toString(response.getEntity(),"UTF8");
             response.close();
             httpClient.close();
        }catch(Exception e){
            logger.error("doGetWithHttps请求异常:{}",e);
        }
        return result;
    }
    
    public static String doGetWithCookie(String url, CookieStore cookieStore){
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClients.custom()
                    .setDefaultCookieStore(cookieStore)
                    .setSSLSocketFactory(sslsf)
                    .setConnectionManager(cm)
                    .setConnectionManagerShared(true)
                    .build();
            /*
             * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
             */
             URIBuilder uriBuilder = new URIBuilder(url);

             // 根据带参数的URI对象构建GET请求对象
             HttpGet httpGet = new HttpGet(uriBuilder.build());

             //执行post请求
             CloseableHttpResponse response = httpClient.execute(httpGet);
             result = EntityUtils.toString(response.getEntity(),"UTF8");
             response.close();
             httpClient.close();
        }catch(Exception e){
            logger.error("doGetWithCookie请求异常:{}",e);
        }
        return result;
    }
}

Jsoup:

(1)一款Java的HTML解析器,主要用来对HTML解析
(2)虽然也支持从某个地址直接去爬取网页源码,但是只支持HTTP,HTTPS协议,支持不够丰富

//pom.xml
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.12.1</version>
</dependency>
//基本用法
Document document = Jsoup.connect(url)
      // 手动设置cookies
      .header("Cookie", "your cookies")
      .get();
System.out.println(document );

爬虫实现思路

在做爬虫时,遇到需要登陆的问题也比较常见,比如写脚本抢票之类的,但凡需要个人信息的都需要登陆,对于这类问题主要有两种解决方式:一种方式是手动设置 cookie ,就是先在网站上面登录,复制登陆后的 cookies ,在爬虫程序中手动设置 HTTP 请求中的 Cookie 属性,这种方式适用于采集频次不高、采集周期短,因为 cookie 会失效,如果长期采集的话就需要频繁设置 cookie,这不是一种可行的办法,第二种方式就是使用程序模拟登陆,通过模拟登陆获取到 cookies,这种方式适用于长期采集该网站,因为每次采集都会先登陆,这样就不需要担心 cookie 过期的问题。

1、模拟登录之Jsoup方式

/**
 * Jsoup 模拟登录豆瓣 访问个人中心
 * 在豆瓣登录时先输入一个错误的账号密码,查看到登录所需要的参数
 * 先构造登录请求参数,成功后获取到cookies
 * 设置request cookies,再次请求
 * @param loginUrl 登录url
 * @param userInfoUrl 个人中心url
 * @throws IOException
 */
public void jsoupLogin(String loginUrl,String userInfoUrl) throws IOException {
 
  // 构造登陆参数
  Map<String,String> data = new HashMap<>();
  data.put("name","your_account");
  data.put("password","your_password");
  data.put("remember","false");
  data.put("ticket","");
  data.put("ck","");
  Connection.Response login = Jsoup.connect(loginUrl)
      .ignoreContentType(true) // 忽略类型验证
      .followRedirects(false) // 禁止重定向
      .postDataCharset("utf-8")
      .header("Upgrade-Insecure-Requests","1")
      .header("Accept","application/json")
      .header("Content-Type","application/x-www-form-urlencoded")
      .header("X-Requested-With","XMLHttpRequest")
      .header("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
      .data(data)
      .method(Connection.Method.POST)
      .execute();
  login.charset("UTF-8");
  // login 中已经获取到登录成功之后的cookies
  // 构造访问个人中心的请求
  Document document = Jsoup.connect(userInfoUrl)
      // 取出login对象里面的cookies
      .cookies(login.cookies())
      .get();
  if (document != null) {
    Element element = document.select(".info h1").first();
    if (element == null) {
      System.out.println("没有找到 .info h1 标签");
      return;
    }
    String userName = element.ownText();
    System.out.println("豆瓣我的网名为:" + userName);
  } else {
    System.out.println("出错啦!!!!!");
  }
}

2、模拟登录之httpclient方式

/**
 * httpclient 的方式模拟登录豆瓣
 * httpclient 跟jsoup差不多,不同的地方在于 httpclient 有session的概念
 * 在同一个httpclient 内不需要设置cookies ,会默认缓存下来
 * @param loginUrl
 * @param userInfoUrl
 */
public void httpClientLogin(String loginUrl,String userInfoUrl) throws Exception{
 
  CloseableHttpClient httpclient = HttpClients.createDefault();
  HttpUriRequest login = RequestBuilder.post()
      .setUri(new URI(loginUrl))// 登陆url
      .setHeader("Upgrade-Insecure-Requests","1")
      .setHeader("Accept","application/json")
      .setHeader("Content-Type","application/x-www-form-urlencoded")
      .setHeader("X-Requested-With","XMLHttpRequest")
      .setHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
      // 设置账号信息
      .addParameter("name","your_account")
      .addParameter("password","your_password")
      .addParameter("remember","false")
      .addParameter("ticket","")
      .addParameter("ck","")
      .build();
  // 模拟登陆
  CloseableHttpResponse response = httpclient.execute(login);
  if (response.getStatusLine().getStatusCode() == 200){
    // 构造访问个人中心请求
    HttpGet httpGet = new HttpGet(userInfoUrl);
    CloseableHttpResponse user_response = httpclient.execute(httpGet);
    HttpEntity entity = user_response.getEntity();
    //
    String body = EntityUtils.toString(entity, "utf-8");
 
    // 偷个懒,直接判断 缺心眼那叫单纯 是否存在字符串中
    System.out.println("缺心眼那叫单纯是否查找到?"+(body.contains("缺心眼那叫单纯")));
  }else {
    System.out.println("httpclient 模拟登录豆瓣失败了!!!!");
  }
}

两者不一样的地方,httpclient 能够像浏览器一样保存 session 会话,这样登陆之后就保存下了 cookie ,在同一个 httpclient 内请求就会带上 cookie

本文部分转载至:https://www.jb51.net/article/171549.htm

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355

推荐阅读更多精彩内容

  • 文章大纲 一、网络爬虫基本介绍二、java常见爬虫框架介绍三、WebCollector实战四、项目源码下载五、参考...
    故事爱人c阅读 1,297评论 0 3
  • HTTP基本原理 URI、URL、URN(Uninform Resource) URI(Identifier):统...
    GHope阅读 2,079评论 2 26
  • 刚进入公司的新人们总是满怀期待,可随着时间的慢慢增长,一切又开始变得有些厌恶起来。于是,每年都有新人欣然加入进来,...
    夏野阅读 190评论 0 0
  • 名字:孑芥子 茕茕孑立:孑:孤单。孤身一人,形容无依无靠,非常孤单。 芥子世界:一花一世界,一叶一如来。佛用来说明...
    孑芥子阅读 633评论 0 3
  • 世界杯,谢谢你给我一个可爱的丈夫 世界杯月,被戏称为“寡妇月”。各路丈夫们仿佛有了放飞自我的理由,八仙过海各显神通...
    不吃鱼的猫伊阅读 444评论 0 1