以下代码发起 Http 请求
public static String doGet(String url) {
logger.info("当前请求URL{},请求方式为GET", url);
String result = null;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
logger.error("处理失败 {}", e.getMessage());
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
return result;
}
将结果转为 JSOUP, 具体要找哪些标签内的内容,可以看下 JSOUP 的文档
//第一步,将内容解析成一个Document类
Document doc = Jsoup.parse(result);
//第二步,根据我们需要得到的标签,选择提取相应标签的内容
String title = doc.getElementsByTag("title").text();
//也可以使用select查询head中的标签
String image = doc.head().select("meta[property=og:image]").attr("content");