本帖最后由 songjing 于 2022-2-28 16:02 编辑
感觉java底子不大好 就想看一下基础类教程补一补
奈何家里网络贼差 只有2G信号 网线又太贵(90一个月)
所以只好下载下来 然后回家慢慢看 就有了这个帖子
所用技术
[Java] 纯文本查看 复制代码 package com.cron.cron.test;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* ClassName: downloadWeb
* Description:
* date: 2022/2/28 10:46
*
* @author robotname a
* @author dev whz
* @since JDK 1.8
*/
public class downloadWeb {
@SneakyThrows
public static void main(String[] args){
String url = "https://www.liaoxuefeng.com/wiki/1252599548343744";
long l = System.currentTimeMillis();
//链接到目标地址
Connection connect = Jsoup.connect(url);
//设置useragent,设置超时时间,并以get请求方式请求服务器
Document document = null;
{
try {
document = connect.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").
timeout(6000).ignoreContentType(true).get();
} catch (IOException e) {
e.printStackTrace();
}
}
Element elementById = document.getElementById("x-sidebar-left");
Element elementById1 = elementById.getElementById("1252599548343744");
Elements a = elementById1.getElementsByTag("a");
for (int i = 0; i < a.size(); i++) {
String href = a.get(i).attr("href");
try {
url = "https://www.liaoxuefeng.com" + href;
Connection connects = Jsoup.connect(url);
document = connects.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").
timeout(6000).ignoreContentType(true).get();
BufferedWriter writer = new BufferedWriter(new FileWriter(a.get(i).text()+".html"));
writer.write(document.toString());
writer.newLine();
} catch (Exception e) {
Thread.sleep(6000);
}
}
System.err.println("下载用时"+(System.currentTimeMillis() - l));
}
}
效果图
还不是很完善 样式并没有拉取 今天先写到这 明天继续 记录一下 加油后续会把css搞定 然后转成md文档 或者pdf的不再让生活苟且
不再与过去和解
|