java爬取html页面

pom.xml

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.7.3</version>
		</dependency>
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MyTest {

	public static void main(String[] args) {
		Document doc=getURLContent("https://www.gd.gov.cn/","UTF-8");
		Elements elementsByClass = doc.select("li a");
//		int elementsByClas=elementsByClass.indexOf(2);
//		Elements links=doc.select("a[title]");
		Map<String,String> map=new HashMap<>();
		List<String> titleList=new ArrayList<>();
		for( Element element : elementsByClass){
//			System.out.println(element);//带有onclick属性的a元素
//			System.out.println(element.select("a").text());//获取带有onclick属性的a元素 标签里面的内容
			String title=element.select("a").attr("title");//获取带有onclick属性的a元素 的title 里面的内容
			//获取带有onclick属性的a元素 的onclick里面的参数信息
			String href="https://www.gdzwfw.gov.cn"+element.select("a").attr("href");
			if(StringUtils.isNotBlank(title)) {
                map.put(title, href);
                titleList.add(title);
            }
		}
//		for(String title:titleList){
//			if(map.containsKey(title)){
//				doc=getURLContent(map.get(title),"UTF-8");
//				Element oneTitle = doc.select("em-title").first();
//				Element time = doc.select("em-infos-time").first();
//				Element address = doc.select("em-infos-address").first();
//			}
//		}
		System.out.println(map);
	}

    public static Document getURLContent(String url, String encoding) {
        StringBuffer content = new StringBuffer();
        try {
            // 新建URL对象
            URL u = new URL(url);
            InputStream in = new BufferedInputStream(u.openStream());
            InputStreamReader theHTML = new InputStreamReader(in, encoding);
            int c;
            while ((c = theHTML.read()) != -1) {
                content.append((char) c);
            }
        }
        // 处理异常
        catch (MalformedURLException e) {
            System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        }
        //将字符内容解析成一个Document类(Jsoup解析html)
        return Jsoup.parse(content.toString());
    }

    public void and()throws Exception {
        try {
            BufferedInputStream input1 = new BufferedInputStream(new FileInputStream(new File("D:\\2.mp3"))); //路径格式:D://java//1.MP3
            BufferedInputStream input2 = new BufferedInputStream(new FileInputStream(new File("D:\\2.mp3")));
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("D:\\3.mp3")));
            byte[] byt = new byte[1024];
            int len;
            while ((len = input1.read(byt)) != -1) {
                out.write(byt, 0, byt.length);
                out.flush();
            }
            while ((len = input2.read(byt)) != -1) {
                out.write(byt, 0, byt.length);
                out.flush();
            }
            input1.close();
            input2.close();
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一名落魄的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值