拿走直接用
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.15</version>
</dependency>
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class ZhiHuHot {
public static void main(String[] args) throws IOException {
String URL="https://www.zhihu.com/billboard";
Document doc= Jsoup.connect(URL).get();
String jsContent = doc.select("script[id=js-initialData]").html();
JSONArray hotList = JSONUtil.parseObj(jsContent).getJSONObject("initialState").getJSONObject("topstory").getJSONArray("hotList");
JSONArray ZhiHuHotList = new JSONArray();
for (int i = 0; i < hotList.size(); i++) {
JSONObject target = hotList.getJSONObject(i).getJSONObject("target");
JSONObject ZhiHuHot = new JSONObject();
ZhiHuHot.put("title",target.getJSONObject("titleArea").get("text"));
ZhiHuHot.put("link",target.getJSONObject("link").get("url"));
ZhiHuHot.put("hot_value",target.getJSONObject("metricsArea").getStr("text").split(" ")[0]);
ZhiHuHotList.add(ZhiHuHot);
}
System.out.println(ZhiHuHotList);
}
}