pire.PDF for Java 10.4.9 現(xiàn)已正式發(fā)布。該版本支持獲取 PDF 文檔中的 Javascript 內(nèi)容,并新增了一個(gè)構(gòu)造方法來解決 PdfInkAnnotation 在瀏覽器里沒有顯示的問題。此外,在提取 PDF 表格和扁平化表單域時(shí)遇到的兩個(gè)問題也已成功被修復(fù)。詳情請查閱以下內(nèi)容。
新功能:
問題修復(fù):
獲取 Spire.PDF for Java 10.4.9 請點(diǎn)擊:
https://www.e-iceblue.cn/Downloads/Spire-PDF-JAVA.html
jsoup 是一款 Java 的 HTML 解析器,可直接解析某個(gè) URL 地址、HTML 文本內(nèi)容。它提供了一套非常省力的 API,可通過 DOM,CSS 以及類似于 jQuery 的操作方法來取出和操作數(shù)據(jù),可操作 HTML 元素、屬性、文本。
jsoup 實(shí)現(xiàn) WHATWG HTML5 規(guī)范,并將 HTML 解析為與現(xiàn)代瀏覽器相同的 DOM。
大多數(shù)情況下,下面給出 3 個(gè)類是我們需要重點(diǎn)了解的。
Jsoup 類是任何 Jsoup 程序的入口點(diǎn),并將提供從各種來源加載和解析 HTML 文檔的方法。 Jsoup 類的一些重要方法如下:
方法 | 描述 |
static Connection connect(String url) | 創(chuàng)建并返回 URL 的連接。 |
static Document parse(File in, String charsetName) | 將指定的字符集文件解析成文檔。 |
static Document parse(String html) | 將給定的 html 代碼解析成文檔。 |
static String clean(String bodyHtml, Whitelist whitelist) | 從輸入 HTML 返回安全的 HTML,通過解析輸入 HTML 并通過允許的標(biāo)簽和屬性的白名單進(jìn)行過濾。 |
Jsoup 類的其他重要方法可以參見 - https://jsoup.org/apidocs/org/jsoup/Jsoup.html
該類表示通過 Jsoup 庫加載 HTML 文檔。可以使用此類執(zhí)行適用于整個(gè) HTML 文檔的操作。 Element 類的重要方法可以參見 - http://jsoup.org/apidocs/org/jsoup/nodes/Document.html 。
HTML 元素是由標(biāo)簽名稱,屬性和子節(jié)點(diǎn)組成。 使用 Element 類,您可以提取數(shù)據(jù),遍歷節(jié)點(diǎn)和操作 HTML。 Element 類的重要方法可參見 - http://jsoup.org/apidocs/org/jsoup/nodes/Element.html 。
實(shí)現(xiàn)解析liuhaihua.cn首頁list
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsoup</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>
package com.et.jsoup;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public Map<String, Object> showHelloWorld(){
Map<String, Object> map=new HashMap<>();
map=JsoupUtil.parseHtml("http://www.liuhaihua.cn/");
map.put("msg", "HelloWorld");
return map;
}
}
package com.et.jsoup;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* @author liuhaihua
* @version 1.0
* @ClassName JsoupUtil
* @Description todo
* @date 2024/06/24/ 9:16
*/
public class JsoupUtil {
public static Map<String ,Object> parseHtml(String url){
Map<String,Object> map=new HashMap<>();
//1.生成httpclient,相當(dāng)于該打開一個(gè)瀏覽器
CloseableHttpClient httpClient=HttpClients.createDefault();
CloseableHttpResponse response=null;
//2.創(chuàng)建get請求,相當(dāng)于在瀏覽器地址欄輸入 網(wǎng)址
HttpGet request=new HttpGet(url);
//設(shè)置請求頭,將爬蟲偽裝成瀏覽器
request.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
// HttpHost proxy=new HttpHost("60.13.42.232", 9999);
// RequestConfig config=RequestConfig.custom().setProxy(proxy).build();
// request.setConfig(config);
try {
//3.執(zhí)行g(shù)et請求,相當(dāng)于在輸入地址欄后敲回車鍵
response=httpClient.execute(request);
//4.判斷響應(yīng)狀態(tài)為200,進(jìn)行處理
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
//5.獲取響應(yīng)內(nèi)容
HttpEntity httpEntity=response.getEntity();
String html=EntityUtils.toString(httpEntity, "utf-8");
System.out.println(html);
/**
* 下面是Jsoup展現(xiàn)自我的平臺
*/
//6.Jsoup解析html
Document document=Jsoup.parse(html);
//像js一樣,通過標(biāo)簽獲取title
System.out.println(document.getElementsByTag("title").first());
Elements blogmain=document.getElementsByClass("col-sm-8 blog-main");
//像js一樣,通過class 獲取列表下的所有博客
Elements postItems=blogmain.first().getElementsByClass("fade-in");
//循環(huán)處理每篇博客
List<Map> list=new ArrayList<>();
for (Element postItem : postItems) {
Map<String,Object> row=new HashMap<>();
//像jquery選擇器一樣,獲取文章標(biāo)題元素
Elements titleEle=postItem.select(".entry-title a");
System.out.println("文章標(biāo)題:" + titleEle.text());;
row.put("title",titleEle.text());
System.out.println("文章地址:" + titleEle.attr("href"));
row.put("href",titleEle.attr("href"));
//像jquery選擇器一樣,獲取文章作者元素
Elements footEle=postItem.select(".archive-content");
System.out.println("文章概要:" + footEle.text());;
row.put("summary",footEle.text());
Elements view=postItem.select(".views");
System.out.println( view.text());
row.put("views",view.text());
System.out.println("*********************************");
list.add(row);
}
map.put("data",list);
} else {
//如果返回狀態(tài)不是200,比如404(頁面不存在)等,根據(jù)情況做處理,這里略
System.out.println("返回狀態(tài)不是200");
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//6.關(guān)閉
HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(httpClient);
}
return map;
}
public static void main(String[] args) {
parseHtml("http://www.liuhaihua.cn/");
}
}
package com.et.jsoup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫
、javaScript介紹
JavaScript是一種基于對象和事件驅(qū)動(dòng)的、并具有安全性能的腳本語言
(客戶端語言)
JavaScript特點(diǎn)
向HTML頁面中添加交互行為
腳本語言,語法和Java類似
解釋性語言,邊解釋邊執(zhí)行
JavaScript組成:ECMAScript 、DOM、BOM
基本結(jié)構(gòu):
<script type="text/javascript">
<!—
JavaScript 語句;
—>
</script >
示例:
……
<title>初學(xué)JavaScript</title>
</head>
<body>
<script type="text/javascript">
document.write("初學(xué)JavaScript");
document.write("<h1>Hello,JavaScript</h1>");
</script>
</body>
</html>
注:<script>…</script>可以包含在文檔中的任何地方,只要保證這些代碼在被使用前已讀取并加載到內(nèi)存即可
執(zhí)行原理:
外部JS文件:
<script src="export.js" type="text/javascript"></script>
直接在HTML標(biāo)簽中使用:
<input name="btn" type="button" value="彈出消息框"
onclick="javascript:alert('歡迎你');"/>
二、基本常見語法:
1、核心語法:同時(shí)聲明和賦值變量
var catName="皮皮";
2、數(shù)據(jù)類型:
undefined:var width;
變量width沒有初始值,將被賦予值undefined;
null:表示一個(gè)空值,與undefined值相等;
number:var iNum=23; //整數(shù)
var iNum=23.0; //浮點(diǎn)數(shù)
boolean:true 和false;
string:一組被引號(單引號或雙引號)括起來的文本
var string1="This is a string";
3、typeof運(yùn)算符:
typeof檢測變量的返回值
typeof運(yùn)算符返回值如下函數(shù)
undefined:變量被聲明后,但未被賦值
string:用單引號或雙引號來聲明的字符串
boolean:true或false
number:整數(shù)或浮點(diǎn)數(shù)
object:javascript中的對象、數(shù)組和nul
4、String對象:
5、數(shù)組:
數(shù)組的常用屬性和方法
類別 名稱 描述
屬性 length 設(shè)置或返回?cái)?shù)組中元素的數(shù)目
方法 join( ) 把數(shù)組的所有元素放入一個(gè)字符串,通過一個(gè)的分隔符進(jìn)行分隔
sort() 對數(shù)組排序
push() 向數(shù)組末尾添加一個(gè)或更多 元素,并返回新的長度
6、邏輯控制語句:
if(條件)
{
//JavaScript代碼;
}
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。