Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
jsoup 是一款Java 的HTML解析器,我們使用它可直接解析URL地址、HTML文本內容。除此之外,它提供了一套非常友好的API,使得我們可以比較便利的通過DOM,CSS等操作類型,獲取對應網頁元素屬性及數據操作。
jsoup官網:https://jsoup.org/
① 從一個URL,文件或字符串中解析HTML
② 使用DOM或CSS選擇器來查找、取出數據使用DOM或CSS選擇器來查找、取出數據
③ 可操作HTML元素、屬性、文本可操作HTML元素、屬性、文本
Tips:jsoup是基于MIT協議發布的,可放心使用于商業項目
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
6個包提供用于開發jsoup應用程序的類和接口。
org.jsoup
org.jsoup.examples
org.jsoup.helper
org.jsoup.nodes
org.jsoup.parser
org.jsoup.safety
org.jsoup.salect
主要類:
Jsoup 類提供了連接,清理和解析HTML文檔的方法
Document 獲取HTML文檔
Element 獲取、操作HTML節點
public static void main(String[] args) {
/* 從URL加載HTML */
Document document=Jsoup.connect("http://www.baidu.com").get();
String title=document.title();
/* 獲取html中的標題 */
System.out.println("title :"+title);
/* 從字符串加載HTML */
String html="<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc=Jsoup.parse(html);
title=doc.title();
System.out.println("title :"+title);
/* 從文件加載HTML */
doc=Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");
title=doc.title();
System.out.println("title :"+title);
}
public static void main(String[] args) {
Document document=Jsoup.connect("http://www.baidu.com").get();
String title=document.title();
System.out.println("title :"+title);
/* 獲取html中的head */
System.out.println(document.head());
/* 獲取html中的body */
/* 獲取HTML頁面中的所有鏈接 */
Elements links=document.select("a[href]");
for (Element link : links){
System.out.println("link : "+ link.attr("href"));
System.out.println("text :"+ link.text());
}
}
public static void main(String[] args) {
Document document=Jsoup.connect("https://passport.lagou.com").get();
System.out.println(document.head());
/* 獲取URL的元信息 */
String description=document.select("meta[name=description]").get(0).attr("content");
System.out.println("Meta description : " + description);
String keywords=document.select("meta[name=keywords]").first().attr("content");
System.out.println("Meta keyword : " + keywords);
}
public static void main(String[] args) {
Document document=Jsoup.connect("https://baidu.com").get();
/* 獲取拉勾網登入頁面的body */
/* System.out.println(document.body()); */
/* 根據class名稱獲取表單 */
Elements formElement=document.getElementsByClass("form_body");
System.out.println(formElement.html());
/* 獲取URL的元信息 */
for (Element inputElement : formElement) {
String placeholder=inputElement.getElementsByTag("input").attr("placeholder");
System.out.println(placeholder);
}
}
public static void main(String[] args) {
Document document=Jsoup.parse(new File("F:\\jsoup\\html\\login.html"),"utf-8");
Element loginform=document.getElementById("registerform");
Elements inputElements=loginform.getElementsByTag("input");
for (Element inputElement : inputElements) {
String key=inputElement.attr("name");
String value=inputElement.attr("value");
System.out.println("Param name: "+key+" -- Param value: "+value);
}
}
public static void main(String[] args) {
Document document=Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");
System.out.println(document.body());
System.out.println("*************");
Element div=document.select("div").first();
div.html("<p>Hello</p>");
div.prepend("<p>Fiest</p>");
div.append("<p>Last</p>");
System.out.println(document.body());
System.out.println(div.text());
/* 對元素包裹一個外部HTML內容 */
div.wrap("<div id=\"div2\"></div>");
System.out.println(document.body());
}
好不要在搜索引擎出現過
title標題最好不在搜索引擎出現過,可以嘗試在搜索引擎進行搜索,適當地改變一下。
一次敲定后不要改動
title寫好了最好不要動,尤其是在網站權重還較低的情況下。很多網站快照不更新就是因為title經常改動。title標題要仔細寫,一次性寫好后短時間內都不要改動,改動標題的風險是很大的。
加上h1標題標簽
title標題在源碼中,要加上h1標題標簽,告訴搜索引擎,突顯它的重要性。
切詞的特殊符號使用
一般我們在網站標題描寫中,經常使用的符號有:“,”(逗號)、“ 空格(空格)、“-”(英文下 的橫線)、“—”(中文下的橫線)、“|”(豎)、“_”(下劃線)、“/”(斜杠)這些符號在標題中所起的作用及表達的意義是不一樣的,不僅不同的分隔符在標題在所起的作用不一樣,即使是同一分隔符在不同的搜索引擎里表達的意義也會有所差別。
控制title字數
title最多顯示30個中文字符。網頁標題中多余的部分搜索引擎都會自動切斷。所以在寫標題標簽的時候,一般控制在30個中文字符以內。標題太長,會分散關鍵詞的權重。
關鍵字搭配
關鍵字搭配是指關鍵字在標題中出現的前后位置、主次搭配。 搜索引擎對于頁面信息的抓取,是由HTML代碼中自上而下進行的, 所以在網頁標題的最前面加上主關鍵字,可以很好地體現頁面的主題內容。
在 HTML 文檔中,標題很重要。
標題(Heading)是通過 <h1> - <h6> 標簽進行定義的。
<h1> 定義最大的標題。 <h6> 定義最小的標題。
實例
<h1>這是一個標題。</h1>
<h2>這是一個標題。</h2>
<h3>這是一個標題。</h3>
注釋: 瀏覽器會自動地在標題的前后添加空行。
請確保將 HTML 標題 標簽只用于標題。不要僅僅是為了生成粗體或大號的文本而使用標題。
搜索引擎使用標題為您的網頁的結構和內容編制索引。
因為用戶可以通過標題來快速瀏覽您的網頁,所以用標題來呈現文檔結構是很重要的。
應該將 h1 用作主標題(最重要的),其后是 h2(次重要的),再其次是 h3,以此類推。
<hr> 標簽在 HTML 頁面中創建水平線。
hr 元素可用于分隔內容。
實例
<p>這是一個段落。</p>
<hr>
<p>這是一個段落。</p>
<hr>
<p>這是一個段落。</p>
可以將注釋插入 HTML 代碼中,這樣可以提高其可讀性,使代碼更易被人理解。瀏覽器會忽略注釋,也不會顯示它們。
注釋寫法如下:
實例
<!-- 這是一個注釋 -->
注釋: 開始括號之后(左邊的括號)需要緊跟一個嘆號,結束括號之前(右邊的括號)不需要,合理地使用注釋可以對未來的代碼編輯工作產生幫助。
你是否看過一些網頁然后驚嘆它是如何實現的。
如果您想找到其中的奧秘,只需要單擊右鍵,然后選擇"查看源文件"(IE)或"查看頁面源代碼"(Firefox),其他瀏覽器的做法也是類似的。這么做會打開一個包含頁面 HTML 代碼的窗口。
菜鳥教程的標簽參考手冊提供了有關這些標題及其屬性的更多信息。
您將在本教程下面的章節中學到更多有關 HTML 標簽和屬性的知識。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。