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 名称
*