1. 程式人生 > >Java | 技術應用 | 利用Jsoup處理頁面

Java | 技術應用 | 利用Jsoup處理頁面

發表 exceptio args ces int 正則表達 find() tid 結合

根據微信公眾號的推文鏈接地址,對文章內容進行爬取,利用jsoup解析文章源代碼,加上結合xpth提取文文章信息,

利用正則表達式讀取文章發表時間。

Jsoup

 <!-- jsoup HTML parser library @ http://jsoup.org/ -->
        <dependency>
          <groupId>org.jsoup</groupId>
          <artifactId>jsoup</artifactId>
          <version>1.10.2</
version> </dependency>

package search;

import java.io.File;
import java.io.IOException;

import java.util.regex.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Files_process {
    
    public
String[] get_content(String path) throws IOException { String[] content = new String[4]; Document document = Jsoup.connect(path).get(); ////*[@id="publish_time"] Elements em = document.select("script"); ////*[@id="img-content"] //*[@id="js_content"]/section
//獲取主體內容 Elements page_content = document.select("div#js_content"); //*[@id="js_name"] //獲取公眾號名稱 Elements cname = document.select("a#js_name"); content[0] = document.title(); //文章標題 content[1] = cname.text(); //公眾號名稱 content[2] = page_content.text(); //文章內容 String code = document.html(); String str = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))"; Pattern pattern = Pattern.compile(str); Matcher matcher = pattern.matcher(code); if(matcher.find()) content[3] = matcher.group(); return content; } }

package search;


public class processed {
    public static void main(String[] args) throws Exception {
          String[] content = null;
    
           Files_process fp = new Files_process();
               
          content =  fp.get_content("http://mp.weixin.qq.com/s?__biz=MjM5NTc5ODM4Ng==&mid=2650901488&idx=1&sn=2a9924f776bc9683ff8e1a1e66fa4214&chksm=bd0627ed8a71aefb07a81e3df3444bb20011ecaaab3050d9f11ccba6f4a66239943dc2784cc4#rd");
          System.out.println("msg_title: "+content[0]);
             System.out.println("nickname: "+content[1]);
             System.out.println("msg_content: "+content[2]);
             System.out.println("msg_time: "+content[3]);
             System.out.println("msg_link: "+"");
             System.out.println();
          
    }
}

Java | 技術應用 | 利用Jsoup處理頁面