百度爬虫爬取官方失信被执行人信息

2018.9.28-修改:使用FastJson解析数据

1.爬取地址及使用的编码:

private static final String UTF_8 = "UTF-8";
private static final String SPIDER_URL = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php";

2.爬取出数据直接返回字符串(注意HTTP传输会乱码,解决方法:http传输乱码-解决new InputStreamReader(response.getEntity().getContent(),UTF_8))

/**
 *  查询出原始数据,返回字符串
 * @param params
 * @return
 */
private static String getStringDishonest(Map<String, Object> params) {
    StringBuffer resultBuffer;
    String fullUrl = null;
    // 创建HTTP请求
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    HttpClient client = httpClientBuilder.build();
    BufferedReader bufferedReader = null;
    // 构建请求参数
    StringBuilder buffer = new StringBuilder();
    if (params != null && params.size() > 0) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            buffer.append(entry.getKey());
            buffer.append("=");
            try {
                buffer.append(URLEncoder.encode((String) entry.getValue(), DishonestUtil.UTF_8));
            } catch (UnsupportedEncodingException e) {
                throw new MyException("失信人查询编码异常:"+e);
            }
            buffer.append("&");
        }
    }
    // 拼接查询URL
    if (buffer.length() > 0) {
        fullUrl = DishonestUtil.SPIDER_URL + "?" + buffer.substring(0, buffer.length() - 1);
    }

    // 请求拼接后的地址获取详细信息
    HttpGet httpGet = new HttpGet(fullUrl);
    try {
        // 获得响应数据
        HttpResponse response = client.execute(httpGet);
        // 读入响应数据体文本信息
        // http传输乱码-解决new InputStreamReader(response.getEntity().getContent(),UTF_8)
        bufferedReader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
        String temp;
        resultBuffer = new StringBuffer();
        while ((temp = bufferedReader.readLine()) != null) {
            resultBuffer.append(temp);
        }
        return resultBuffer.toString();
    } catch (IOException e) {
        logger.error("失信工具IO异常:"+e.getMessage());
        throw new MyException("失信工具IO异常:"+e);
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                logger.error("失信工具IO关闭异常:"+e.getMessage());
            }
        }
    }
}

3.对字符串进行转换为Json然后读入类中:

/**
 * 查询失信人
 * @param performedName 名称
 * 
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值