JAVA 爬东西真好用

package cn.luke.tool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class digger {

	public static void main(String[] args) throws IOException {
		String base = "F:\\logs\\";
		String path = base+"a.txt";
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(path));
			String BaseUrl = "http://serv.icem.e.d.calcs.med.live.cn/web/";
			bw.write(BaseUrl);
			bw.newLine(); // 换行用
			Document document = Jsoup.connect(BaseUrl).timeout(10000).ignoreContentType(true).userAgent(
					"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
					.get();
			Elements h3 = document.select("h3");
			Elements h4 = document.select("h4");
			Elements element = document.select("ul");
			int size = element.size();
			System.out.println(h3.get(0).text());
			for (int i = 0; i < size; i++) {
				System.err.println(h4.get(i).text() + "---" + element.get(i).childNodeSize());
			}
			int[] i = new int[1];
			i[0] = 0;
			element.forEach(em -> {
				Elements a_element = em.select("a");
				a_element.forEach(ae -> {
					try {
//					if (++i[0] < 2) {
						String href = ae.attr("href");
						if (href.startsWith("sh...ow.php?")) {
							String title = ae.select("li").text();
							System.out.println("href." + href);
							System.out.println("title" + title);
							bw.newLine(); // 换行用
							bw.write(title);
							bw.newLine(); // 换行用
							bw.write(href);
							bw.newLine(); // 换行用
							Document sub = Jsoup.connect(BaseUrl+"/"+href).timeout(10000).ignoreContentType(true).userAgent(
									"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
									.get();
							String[] html =new String[1];
							html[0] = sub.html(); 
							Elements js =  sub.select("script");
							js.forEach(jse->{
								if(!jse.attr("type").isEmpty()) {
									html[0]  = html[0] .replace(jse.outerHtml(),"");							
								}else {
									String jsFile = jse.attr("src");
									String JsSavePath = base+jsFile.replace("calc.ul.ate/",File.separator+"tool"+File.separator+"tools_").replace("/", File.separator);
									System.out.println(jsFile);
									System.out.println(JsSavePath);
									File files = new File(JsSavePath);
									if (!files.exists()) {
										createFile(JsSavePath,base);
									}
									try {
										BufferedWriter bww = new BufferedWriter(new FileWriter(JsSavePath));
										Document jsFileD = Jsoup.connect(BaseUrl+"/"+jsFile).timeout(10000).ignoreContentType(true).userAgent(
												"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
												.get();
										bww.write(jsFileD.body().text());
										bww.newLine(); // 换行用
										bww.close();
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
									html[0]  = html[0].replace("calculate/","tools_");
								}
							});
							Elements css =  sub.select("link");
							css.forEach(csse->{
								String _href = csse.attr("href");
								String cssSavePath = base+"tool"+File.separator+_href.replace("cal.cu.late/",File.separator+"tool"+File.separator+"tools_").replace("/", File.separator);
								System.out.println(cssSavePath);
								File files = new File(cssSavePath);
								if (!files.exists()) {
									createFile(cssSavePath,base);
								}
								try {
									BufferedWriter bww = new BufferedWriter(new FileWriter(cssSavePath));
									Document jsFileD = Jsoup.connect(BaseUrl+"/"+_href).timeout(10000).ignoreContentType(true).userAgent(
											"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
											.get();
									bww.write(jsFileD.body().text());
									bww.newLine(); // 换行用
									bww.close();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							});
							String htmlName = href.substring(href.indexOf("=")+1,href.length());
							File htmlSave = new File(base+"tool"+File.separator+htmlName+".html");
							if (!htmlSave.exists()) {
								createFile(base+"tool"+File.separator+htmlName+".html",base);
							}
							BufferedWriter htmlbw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(base+"tool"+File.separator+htmlName+".html")),"utf-8"));
							htmlbw.write(html[0]);
							htmlbw.newLine(); // 换行用
							htmlbw.close();
						}
//					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				});
			});
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建文件
	 * 
	 * @param destFileName 文件路劲
	 * @return Boolean
	 */
	public static boolean createFile(String destFileName,String base) {
		if(destFileName.equals(base) || destFileName.endsWith(File.separator)) {
			return true;
		}
		File file = new File(destFileName);
		if (file.exists()) {
			System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
			return false;
		}
		// 判断目标文件所在的目录是否存在
		if (!file.getParentFile().exists()) {
			// 如果目标文件所在的目录不存在,则创建父目录
			System.out.println("目标文件所在目录不存在,准备创建它!");
			if (!file.getParentFile().mkdirs()) {
				System.out.println("创建目标文件所在目录失败!");
				return false;
			}
		}
		// 创建目标文件
		try {
			if (file.createNewFile()) {
				System.out.println("创建单个文件" + destFileName + "成功!");
				return true;
			} else {
				System.out.println("创建单个文件" + destFileName + "失败!");
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
			return false;
		}
	}
}

   

<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.11.3</version>
</dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彳卸风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值