[java]通过网页class获取标签内容

这里我编写了两个类,一个是CatHttp,一个是测试代码用的类。 CatHttp主要是用来写调用方法的,简化测试代码的代码量。
这里那b站做例子,pc端网页,首先访问网页获取页面源代码

- 访问并获取网页源码

//CatHttp类
	public String getHttpText(String Url) {
		// TODO Auto-generated method stub
		try {

			// 获取url
			url = new URL(Url);
			// 下载资源
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36");
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String msg = null;
			String urlString = "";
			while (null != (msg = br.readLine())) {
				urlString += msg + "\n";
			}
//			System.out.println(urlString);
			br.close();
			return urlString;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

在这里插入图片描述

获取这个class的内容,写个内容截取方法,截取方法我做了多个判断,以便使用。
还有一点就是用来索引的字符会在返回时去除,这么做的原因是因为我以前在一款编程软件使用的习惯,而substring方法+indexof方法索引的字符会去尾留头。这样在获取详细信息时可能会造成一些美观上的问题。()。

  • 截取方法
public String interception(String nr, String str1, String str2) {
		// 传入数据, 截取内容,并返回截取结果
		try {

			String interception = null;
			if (str2 == null) {
				interception = nr.substring(nr.indexOf(str1));
				return interception.replace(str1, "");

			} else if (str1 == null) {
				return interception = nr.substring(0, nr.indexOf(str2));
			} else {
				nr = nr.substring(nr.indexOf(str1));
				interception = nr.substring(0, nr.indexOf(str2));
				return interception.replace(str1, "");
			}
		} catch (StringIndexOutOfBoundsException e) {
			// TODO: handle exception
			System.err.println("超出数组或索引范围");
		} catch (NullPointerException e) {
			System.err.println("引用对象为空");
		}
		return null;

	}

调用截取方法执行返回
在这里插入图片描述
内容获取成功,但是我需要获取的可不止一条内容,而是所有包含该class的标签的内容。

  • 列表添加和读取方法
  • CatHttp类
 private LinkedList<String> list = null;
 public void setList(String str1, String str2) {
		// 传入一个数据,设置一个分割符并转化为列表形式
		list = new LinkedList<String>();
		String[] temp = str1.split(str2); // 分割字符串
		for (int i = 0; i < temp.length; i++) {
			list.add(temp[i]);
		}
	}

	public LinkedList<String> getList() {
		return list;
	}
 
  • 测试代码类
 for (String cat : h.getList()) {
			 System.out.println(cat);
			 System.out.println("-----------------------");
			 
		 }

写完这些方法之后该就该传入数据了,根据网页的class获取标签并获取标签内的内容,还要传入一个字符串作为数组的分隔符。

- 通过class获取标签内容完整代码

public void getLabcont(String nr, String Class) {
		// 获取class内容方法
		String cont = null;
		String n = "";
		String lab = null;
		boolean is = false;
		this.setList(nr, "<");
		for (String cat : this.getList()) {
//			System.out.println(cat);
			if (is == false) {
				if (cat.indexOf(Class) > -1) {
					cat = "<" + cat;
					cont = this.interception(cat, "<", Class);
					cont = this.interception(cont, null, " ");
//					lab = "</" + cont + ">";
					n = n + cat;
					is = true;
				} else {
					is = false;
				}
			} else if (is == true) {
				if (cat.indexOf(cont) > -1) {
					lab = "</" + cont + ">";
					n = n + cat + lab;
					cont = this.interception(nr, cont, "\">");
					is = false;
				} else {
					n = n + cat;
//					System.out.println(n);

				}
			}
		}
// System.out.println(n);
		this.setList(n, lab);

	}
}

代码解释一下,因为在前面说过传入的截取用的字符串会被去除,所以我这里做了补全,顺便再在补全的基础上加上了用来作为分隔符的字符串^_<

代码执行结果如下
在这里插入图片描述

都已经做到这一步了,再再表演一下截取方法的用法

- 格式化输出内容

for (String cat : h.getList()) {
			url = h.interception(cat, "a href=\"//", "\" target=\"");
			jpg = h.interception(cat, "img src=\"//", "\" alt=\"");
			zuo = h.interception(cat, " alt=\"","\">div class");
			System.out.println("视频标题:"+zuo+"\n视频封面:"+jpg+"\n视频链接地址:"+url+"\n\n");

		}

程序执行结果如图:
在这里插入图片描述
这样实现方法后差不多等同于一个简单得爬虫程序了,嘿嘿嘿。
完整代码下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值