使用jsoup爬取了下某个网站中的《冰与火之歌》信息,并将格式保存成了json格式到文本文件中。
具体执行的代码如下:
public static void main(String[] args) throws IOException {
TestJsoupBingYuHuo tj = new TestJsoupBingYuHuo();
tj.test();
}
static String urlPath = "http://www.bingyuhuozhige.cc";
static String srcPath = "D:\\study\\jsoup\\bingyuhuozhige\\";
static boolean writeOnOff = true;
public void test() throws IOException {
Document document = JsoupUtils.getRoot(urlPath);
Elements h3lists = document.select("h3");
this.analysisH3List(h3lists);
}
// 解析册信息
private JSONArray analysisH3List(Elements h3List) throws IOException {
int n = 0;
for (Element h3 : h3List) {
n++;
if (n < 7) { //改变这里的数字,可以设置从第几册开始跑
continue;
}
// h3 是册名
String h3Text = h3.text().trim();
System.out.println(h3Text);
if(writeOnOff) {
FileUtils.createDir(srcPath + h3Text); // 创建册