这篇文章主要介绍了Java获取任意http网页源代码的方法,可实现获取网页代码以及去除HTML标签的代码功能,涉及Java正则操作相关实现技巧,需要的朋友可以参考下
本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:
JAVA获取任意http网页源代码可实现如下功能:
1. 获取任意http网页的代码
2. 获取任意http网页去掉HTML标签的代码
Webpage类:
/** * 网页操作相关类 */ package test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author winddack * */ public class Webpage { private String pageUrl;//定义需要操作的网页地址 private String pageEncode="UTF8";//定义需要操作的网页的编码 public String getPageUrl() { return pageUrl; } public void setPageUrl(String pageUrl) { this.pageUrl = pageUrl; } public String getPageEncode() { return pageEncode; } public void setPageEncode(String pageEncode) { this.pageEncode = pageEncode; } //定义取源码的方法 public String getPageSource() { StringBuffer sb = new StringBuffer(); try { //构建一URL对象 URL url = new URL(pageUrl); //使用openStream得到一输入流并由此构造一个BufferedReader对象 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), pageEncode)); String line; //读取www资源 while ((line = in.readLine()) != null) { sb.append(line); } in.close(); } catch (Exception ex) { System.err.println(ex); } return sb.toString(); } //定义一个把HTML标签删除过的源码的方法 public String getPageSourceWithoutHtml() { final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式 final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式 final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 String htmlStr = getPageSource();//获取未处理过的源码 Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); Matcher m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 过滤script标签 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); Matcher m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 过滤style标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); Matcher m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 过滤html标签 Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE); Matcher m_space = p_space.matcher(htmlStr); htmlStr = m_space.replaceAll(""); // 过滤空格回车标签 htmlStr = htmlStr.trim(); // 返回文本字符串 htmlStr = htmlStr.replaceAll(" ", ""); htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1); return htmlStr; } }
调用:
Webpage page=new Webpage(); page.setPageUrl("http://www.baidu.com"); String code=page.getPageSourceWithoutHtml(); System.out.println(code);
The above is the detailed content of Java method to obtain the source code of any http web page. For more information, please follow other related articles on the PHP Chinese website!

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

JRE is the environment in which Java applications run, and its function is to enable Java programs to run on different operating systems without recompiling. The working principle of JRE includes JVM executing bytecode, class library provides predefined classes and methods, configuration files and resource files to set up the running environment.

JVM ensures efficient Java programs run through automatic memory management and garbage collection. 1) Memory allocation: Allocate memory in the heap for new objects. 2) Reference count: Track object references and detect garbage. 3) Garbage recycling: Use the tag-clear, tag-tidy or copy algorithm to recycle objects that are no longer referenced.

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor