java正则表达式解析html示例分享
发布时间:2023-06-13 15:53:42
package work;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class chuanboyi {
public static void main(String[] args){
// TODO Auto-generated method stub
StringBuffer html = new StringBuffer();
HttpClient httpclient = new HttpClient();
//创建GET方法实例
GetMethod getMethod = new GetMethod("https://www.jb51.net");
//使用系统提供的默认恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try{
//执行GET方法
int statusCode = httpclient.executeMethod(getMethod);
if(statusCode != HttpStatus.SC_OK){
System.out.println("Method is wrong " + getMethod.getStatusLine());
}
InputStream responseBody = getMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"utf-8"));
String line = reader.readLine();
while(line != null){
html.append(line).append("\n");
line = reader.readLine();
}
reader.close();
//正则表达式
String regex = "<form name=\"compareForm\"[\\s\\S]+>[\\s\\S]+</form>.*<script.*>";
String regexa ="(?<=<li>)[\\s\\S]+?(?=</li>)";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(html);
StringBuffer str = new StringBuffer();
int i = 0;
while(m.find()){
str.append(m.group());
}
pattern = Pattern.compile(regexa);
m = pattern.matcher(str);
while(m.find()){
attrs(m.group());
i++;
}
System.out.println("共有"+i+"条数据!");
}catch (HttpException e) {
// TODO: handle exception
System.out.println("Please check your provided http address!");
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
System.out.println("the line is wrong!");
e.printStackTrace();
}finally{
getMethod.releaseConnection();//释放链接
}
}
public static void attrs(String str){
//获取url的正则表达式
String regexURL = "[a-z]+-[0-9]+\\.html";
//获取Name的正则表达式
String regexName = "(?<=title=\")[[\\w-\\s][^x00-xff]]+(?=\")";
//获取图片的正则表达式
String regexPicture = "images.*\\.jpg";
Pattern patternURL = Pattern.compile(regexURL);
Pattern patternName = Pattern.compile(regexName);
Pattern patternPicture = Pattern.compile(regexPicture);
Matcher mURL = patternURL.matcher(str);
Matcher mName = patternName.matcher(str);
Matcher mPicture = patternPicture.matcher(str);
if(mName.find()){
System.out.println("名字:"+mName.group());
}
if(mURL.find()){
System.out.println("链接:"+mURL.group());
}
if(mPicture.find()){
System.out.println("图片:"+mPicture.group());
}
}
}


猜你喜欢
- 一. Python中表示时间的两种方式:时间戳:相对于1970.1.1 00:00:00以秒计算的偏移量,唯一的时间元组struct_tim
- 学习网络爬虫难免遇到使用代理的情况,下面介绍一下如何使用requests设置代理:如果需要使用代理,你可以通过为任意请求方法提供 proxi
- 本文实例讲述了Python使用sorted排序的方法。分享给大家供大家参考,具体如下:# 例1. 按照元素出现的次数来排序seq = [2,
- 有时候有很多逗号,这样我们就不好处理了,下面的函数就是将多个逗号替换为一个逗号,方便后面的处理。<script language=&q
- 一、修改Linux默认的IO调度算法.linux默认的IO调度算法为cfq,需要修改为dealine,如果是SSD或者PCIe-SSD设备,
- 过滤器模板层对变量的操作实际还有很多,过滤器就是其中一种。学过Linux系统的一定知道管道操作符,其可以将上一步输出直接作为下一步输入进行处
- 1.where语法和用法(1)语法:where <criteria> 即where <查询条件>具体查询语句:sel
- 在go语言的源码中,会发现很多,代码只有函数签名,却看不到函数体,如:// src/os/proc.go 68行func runtime_b
- 前言:大部分的文件上传功能都是用input标签实现,这样就完全可以把它看作一个输入框,可以通过send_keys()指定文件进行上传了。本章
- 一、前言 JDK(Java Development Kit )是一切java应用程序的基础,可以说,所有的java应用程序是构建
- 初级画心学Python,感觉你们的都好复杂,那我来个简单的,我是直接把心形看作是一个正方形+两个半圆:于是这就很简单了,十行代码解决:imp
- 前言开发中我们或多或少会涉及到一些场景需要使用延时操作,而延时操作其实并不是一个很好的选择,并不是一个很好的方案,因为它不可控,也可能产生时
- import numpy as npimport pandas as pdfrom pandas_datareader import dat
- 1.join()的用法:使用前面的字符串.对后面的列表进行拼接,拼接结果是一个字符串# lst = ["alex",&q
- 现在网上出现了很多在线换底色的网页版工具是这么做的呢?其实用Python就可以实现。环境要求Python3 numpy函数库 opencv库
- 可能某次不小心改了配置文件,导致无法打开jupyter,找了很多方法,都没从根本上解决问题。倒是发现启动的默认目录被改了,怀疑是这个问题。然
- 查询语句的优化是SQL效率优化的一个方式,可以通过优化sql语句来尽量使用已有的索引,避免全表扫描,从而提高查询效率。最近在对项目中的一些s
- 在main.js中按需引入import { Toast} from 'vantVue.use(Toast)页面中的methods中使
- 1.安装pm2 : npm install pm2 -gd这时在命令行下执行pm2命令可能找不到,需要执行如下命令1.创建软链接:ln -s
- 安装Python环境ANACONDA是一个Python的发行版本,包含了400多个Python最常用的库,其中就包括了数据分析中需要经常使用