<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency>
1 package com.example.model; 2 3 public class NewBean { 4 private String title; 5 private String content; 6 private String imgUrl; 7 private String urlA; 8 9 public NewBean() { 10 11 } 12 13 public NewBean(String title, String content, 14 String imgUrl, String urlA) { 15 super(); 16 this.title = title; 17 this.content = content; 18 this.imgUrl = imgUrl; 19 this.urlA = urlA; 20 } 21 22 public String getTitle() { 23 return title; 24 } 25 26 public void setTitle(String title) { 27 this.title = title; 28 } 29 30 public String getContent() { 31 return content; 32 } 33 34 public void setContent(String content) { 35 this.content = content; 36 } 37 38 public String getImgUrl() { 39 return imgUrl; 40 } 41 42 public void setImgUrl(String imgUrl) { 43 this.imgUrl = imgUrl; 44 } 45 46 public String getUrlA() { 47 return urlA; 48 } 49 50 public void setUrlA(String urlA) { 51 this.urlA = urlA; 52 } 53 54 @Override 55 public String toString() { 56 return "NewBean:[title=" + title + ", content=" + content + ", imgUrl=" + imgUrl + "urlA" + urlA + "]"; 57 } 58 }
1 package com.example.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import java.io.IOException; 10 11 import org.jsoup.Jsoup; 12 import org.jsoup.nodes.Document; 13 import org.jsoup.nodes.Element; 14 import org.jsoup.select.Elements; 15 16 import com.alibaba.fastjson.JSON; 17 import com.example.model.NewBean; 18 19 @SpringBootApplication 20 @RestController 21 public class SpringTestApplication { 22 23 @RequestMapping("hello") 24 public String hello(){ 25 return "hello"; 26 } 27 28 29 @RequestMapping("json") 30 public static String getNew(int i) { 31 String url = "http://www.cnmo.com/news/all_" + i + ".html"; 32 List<NewBean> list_bean = new ArrayList<>(); 33 NewBean newbean; 34 try { 35 Document doc = Jsoup.connect(url).get(); 36 // 獲取class等於Newcon的div標籤 37 Element contents = doc.select("div.Newcon").first(); 38 Elements content = contents.getElementsByClass("Newcon-list"); 39 for (Element element : content) { 40 Elements linka = element.getElementsByTag("a"); 41 String linkHref = linka.get(0).attr("href"); 42 String linkText = linka.get(0).text(); 43 Elements linkimg = element.getElementsByTag("img"); 44 String linkSrc = linkimg.get(0).attr("src"); 45 Elements linkp = element.getElementsByTag("p"); 46 String linktxt = linkp.get(0).text(); 47 // 這裏把內部類修飾爲static因此直接new 48 newbean = new NewBean(linkText, linktxt, linkSrc, linkHref); 49 list_bean.add(newbean); 50 } 51 // 使用了阿里的fastJson,其它json框架也能夠,true是格式化 52 String json = JSON.toJSONString(list_bean, true); 53 return json; 54 } catch (IOException e) { 55 // e.printStackTrace(); 56 return null; 57 } 58 } 59 60 61 }
package com.example.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } }
:matchesOwn(regex) 使用正則表達式找到自身的文本 html