Jsoup后台解析html、jsp网页

在一些网络爬虫或者从第三方网站抓取信息的程序都面临1个问题,如何从网页中把所需的信息提取出来,Jsoup是个比较好的选择,它能把网站内容解析成Document,再从document中取element就是个简单的事了。这里介绍1下Jsoup的基本用法。

首先需要下载jar包,jsoup-1.9.2.jar

1、Jsoup解析字符串

public void parseString()
        {
                String html = "<html><head><script type=\"text/javascript\">var date = new Date();alert(date);function sub(u){var token = document.getElementById(\"token\").value;var durl = u + \"token=\" + encodeURIComponent(token);window.open(durl);}</script></head><body><br/><br/>token:<input type=\"text\" name=\"token\" token\" width:500\" value=\"uGyUoJ8A6+ETMgIVYAHTpt/l/cY=\"/></input> <br/><br/><input class='butt' type=\"button\" value=\"打开本地\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/setting/setting!index.action?')\" left:200\"></input><input type=\"button\" value=\"打开44\" name=\"sub\" onclick=\"sub('http://10.30.20.44:8181/mapbar-fieldwork/setting/setting!index.action?')\" left:200\"></input><input type=\"button\" value=\"获取权限接口\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/newaccount/newaccount!queryUserPermissions.action?project)\" left:200\"></input><input type=\"button\" value=\"获取权限接口\" name=\"sub\" onclick=\"sub('http://localhost:8080/mapbar-fieldwork/newaccount/newaccount!queryProjectPermissions.action?project)\" left:200\"></input></body></html>";
                
                //Jsoup解析html
                Document doc =Jsoup.parse(html,"utf-8");
                
                //根据id获取元素
                Element e1 = doc.getElementById("token");
                
                //根据属性获取元素s
                Elements e2s = doc.getElementsByAttribute("onclick");
                
                //根据属性+属性值
                Elements e3s = doc.getElementsByAttributeValue("type", "text");
                
                //根据class
                Elements e4s = doc.getElementsByClass("butt");
                
                //根据 标签
                Elements e5s = doc.getElementsByTag("head");
                
                Elements e6s = doc.select("input[type]");
                
                p(e6s); 
                
        }

2、Jsoup解析url

Jsoup可以直接解析1个网址,把网站的返回内容解析出来

public void parseUrl()
        {
                try 
                {
                        URL url = new URL("http://www.baidu.com");
                        Document doc = Jsoup.parse(url, 1000);
                        Elements e1s = doc.select("a[href=http://news.baidu.com]");
                        p(e1s);
                } catch (IOException e) 
                {
                        e.printStackTrace();
                }
        }

3、Jsoup解析本地文件

可以把html文件解析出来

public void parseFile()
        {
                File file = new File("C:/Users/Administrator/Desktop/测试页面.html");
                try {
                        Document doc = Jsoup.parse(file, "GBK");
                        p(doc);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
public static void p(Object o)
        {
                System.out.println(o);
        }