Java爬虫--Jsoup内容解析

能用Jsoup实现什么?

  • 从URL,文件或字符串中获取并解析HTML
  • 查找和提取数据,使用DOM遍历或CSS选择器
  • 操纵HTML元素,属性和文本
  • 根据安全的白名单清理用户提交的内容,以防止XSS攻击

输出整洁的HTML

《一、从URL加载文档》

1.打开JsoupDemo.java,获取http://www.ygdy8.net/html/gndy/index.html页面的“国内电影下载排行“,首先通过Jsoup类中的静态方法connect返回Document对象,该document对象实际为整个html页面内容。

2.我们想要的国内电影下载排行榜,所对应的class样式为co_content2,可以通过属性、后代选择器选中元素" div[class=‘co_content2’] ul a" ,然后通过Element类中的text()方法获取文本内容.

主要代码:

 1 public static void URLLoader(String url) {
 2         //1、从URL加载文档,使用Jsoup.connect()方法从URL加载HTML
 3         Document d1;
 4         try {
 5             d1 = Jsoup.connect(url).get();
 6             System.out.println(d1.title());
 7             Elements provinces=d1.select(" div[class='co_content4'] ul a");
 8             for (Element ele : provinces) {
 9                 System.out.println(ele.text());
10             }
11         } catch (IOException e) {
12             // TODO Auto-generated catch block
13             e.printStackTrace();
14         }
15        
16     }

《二、从文件加载文档》

在JsoupDemo类中定义方法LocLoader,从本地文件加载文档,获取淘宝主营区域包括哪些方面

先来分析网页结构,发现淘宝的导航菜单用一个div包裹,div里面是ul-li,ul的class为service-bd所以通过选择器“ul[class='service-bd'] li a”即可选中菜单内容

关键代码:

 1 public static void LocLoader(String address){
 2         //2、从文件加载文档,使用Jsoup.parse()方法从文件加载HTML
 3         Document d2;
 4         try {
 5             d2 = Jsoup.parse( new File(address) , "utf-8" );
 6             System.out.println(d2.title());
 7             Elements provinces=d2.select("ul[class='service-bd'] li a");
 8             for (Element ele : provinces) {
 9                 System.out.println(ele.text());
10             }
11         } catch (IOException e) {
12             // TODO Auto-generated catch block
13             e.printStackTrace();
14         }
15         
16     }

《三、从String加载文档》

在JsoupDemo类中定义方法StringLoader(),从字符串中解析内容

 1 public static void StringLoader(){
 2         //3、从String加载文档,使用Jsoup.parse()方法从字符串加载HTML
 3         String html = "<html>"
 4                         + "<head>"
 5                             + "<title>First parse</title>"
 6                         + "</head>"
 7                         + "<body>"
 8                             + "<p>Parsed HTML into a doc.</p>"
 9                             + "<a href='http://www.baidu.com'>百度一下</a>"
10                         + "</body>"
11                     + "</html>";
12         Document d3 = Jsoup.parse(html);
13         String url = d3.select("a").attr("href");
14         System.out.println(d3.title()+"    "+url);
15     }

《四、获取页面所有链接》

 1 public static void allurlLoader(String address){
 2         //4、获取HTML页面中的所有链接
 3         Document d4;
 4         try {
 5             d4 = Jsoup.connect(address).get();
 6             Elements links = d4.select("a[href]");  
 7             for (Element link : links) {
 8                  System.out.println("text : " + link.text()+"---》link : " + link.attr("href"));  
 9             }
10         } catch (IOException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13         }
14     }

源代码:

 1 package com.zyk.jsoup;
 2 import java.io.File;
 3 import java.io.IOException;
 4 
 5 import org.jsoup.Jsoup;
 6 import org.jsoup.nodes.Document;
 7 import org.jsoup.nodes.Element;
 8 import org.jsoup.select.Elements;
 9 
10 public class JsoupDemo {
11     public static void main(String[] args) throws IOException {
12         //JsoupDemo.URLLoader("http://www.ygdy8.net/html/gndy/index.html");
13         //JsoupDemo.LocLoader("src/index.html");
14         JsoupDemo.StringLoader();
15         //JsoupDemo.allurlLoader("https://www.jd.com/");
16     }
17     
18     public static void allurlLoader(String address){
19         //4、获取HTML页面中的所有链接
20         Document d4;
21         try {
22             d4 = Jsoup.connect(address).get();
23             Elements links = d4.select("a[href]");  
24             for (Element link : links) {
25                  System.out.println("text : " + link.text()+"---》link : " + link.attr("href"));  
26             }
27         } catch (IOException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31     }
32     
33     
34     public static void StringLoader(){
35         //3、从String加载文档,使用Jsoup.parse()方法从字符串加载HTML
36         String html = "<html>"
37                         + "<head>"
38                             + "<title>First parse</title>"
39                         + "</head>"
40                         + "<body>"
41                             + "<p>Parsed HTML into a doc.</p>"
42                             + "<a href='http://www.baidu.com'>百度一下</a>"
43                         + "</body>"
44                     + "</html>";
45         Document d3 = Jsoup.parse(html);
46         String url = d3.select("a").attr("href");
47         System.out.println(d3.title()+"    "+url);
48     }
49     
50     
51     public static void LocLoader(String address){
52         //2、从文件加载文档,使用Jsoup.parse()方法从文件加载HTML
53         Document d2;
54         try {
55             d2 = Jsoup.parse( new File(address) , "utf-8" );
56             System.out.println(d2.title());
57             Elements provinces=d2.select("ul[class='service-bd'] li a");
58             for (Element ele : provinces) {
59                 System.out.println(ele.text());
60             }
61         } catch (IOException e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         
66     }
67     
68     
69     
70     public static void URLLoader(String url) {
71         //1、从URL加载文档,使用Jsoup.connect()方法从URL加载HTML
72         Document d1;
73         try {
74             d1 = Jsoup.connect(url).get();
75             System.out.println(d1.title());
76             Elements provinces=d1.select(" div[class='co_content4'] ul a");
77             for (Element ele : provinces) {
78                 System.out.println(ele.text());
79             }
80         } catch (IOException e) {
81             // TODO Auto-generated catch block
82             e.printStackTrace();
83         }
84        
85     }
86     
87     
88 }

猜你喜欢

转载自www.cnblogs.com/2312947032zyk/p/10372483.html