這兩天研究了下youku的視頻路徑,學習了下Document,Elements 做個備份。
package com.zx.com.cn.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* java實現爬蟲
*/
public class Dss {
/**
* 獲取url源(只能獲取當前的第一頁,分頁後的數據獲取不到。)
* @param urlyuan url路徑 "http://list.youku.com/albumlist/show/id_27558795.html?spm=a2h0k.8191403.0.0&sf=10101"
* @param toFilePath 生成文件的路徑 "G:\\indext.html"
*/
@SuppressWarnings("unused")
private static void getUrlYuan(String urlyuan,String toFilePath) {
File des = new File(toFilePath);
if (!des.exists()) { // 判斷是否存在,不存在就創建
try {
// 創建文件
des.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
URL url=new URL(urlyuan);
BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));
BufferedWriter writer=new BufferedWriter(new FileWriter(toFilePath));
String line;
while((line=reader.readLine())!=null){
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 截取指定字段
*/
public static String subString(String str, String strStart, String strEnd) {
/* 找出指定的2個字符在 該字符串裏面的 位置 */
int strStartIndex = str.indexOf(strStart)+3;
int strEndIndex = str.indexOf(strEnd);
/* index爲負數 即表示該字符串中沒有該字符 */
/*//此方法中明確存在指定的兩個字符。
* if (strStartIndex < 0) { return "字符串 :" + str + " 不存在 " + strStart +
* ", 無法截取目標字符串"; } if (strEndIndex < 0) { return "字符串 :" + str + " 不存在 " +
* strEnd + ", 無法截取目標字符串"; }
*/
/* 開始截取 */
String result = str.substring(strStartIndex, strEndIndex);
return result;
}
/**
* 將數據(字符串)寫入文檔。
* @param line
* @param toFilePath
*/
private static void toFile(String line, String toFilePath) {
File des = new File(toFilePath);
if (!des.exists()) { // 判斷是否存在,不存在就創建
try {
// 創建文件
des.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(toFilePath));
System.out.println("line = "+line);
writer.write(line);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 從一個網站獲取和解析一個HTML文檔(只能解析第一頁的數據)
* @param url http://list.youku.com/albumlist/show/id_27558795.html?spm=a2h0k.8191403.0.0&sf=10101
* 真實路徑: "http://player.youku.com/embed/"+id
*/
@SuppressWarnings("unused")
private static void getHTML(String url) {
StringBuffer linkSB=new StringBuffer();
try {
Document doc = Jsoup.connect(url).get();
//數據提取
Elements links = doc.select("div.p-thumb").select("a[href]"); //div class:p-thumb 下的帶有href屬性的a元素
for (Element link : links) {
String linkHref = link.attr("href");//獲取href中的數據。
System.out.println("linkHref = "+linkHref);
//截取id 拼接真實路徑
String id = subString(linkHref, "id_", ".html");
String realURL = "http://player.youku.com/embed/"+id;
System.out.println("realURL = " +realURL);
linkSB.append(realURL);
linkSB.append("\r\n");
}
toFile(linkSB.toString(), "G:\\test.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解析文件位於網站的本地文件系統
* @param localFilePath 本地文件路徑
*
* parse(input, "UTF-8", "http://example.com/")
* parse(File in, String charsetName, String baseUri) baseUri 參數用於解決文件中URLs是相對路徑的問題。如果不需要可以傳入一個空的字符串。
*/
private static void getLocalFile(String localFilePath) {
StringBuffer linkSB=new StringBuffer();
Document doc =null;
if(localFilePath!=null && localFilePath!="") {
try {
File input = new File(localFilePath);
doc = Jsoup.parse(input, "UTF-8", "");
} catch (IOException e) {
e.printStackTrace();
}
}
//數據提取
//Element content = doc.getElementById("content");
//Elements links = content.getElementsByTag("a");
//數據提取
Elements links = doc.select("div.p-thumb").select("a[href]"); //div class:p-thumb 下的帶有href屬性的a元素
for (Element link : links) {
String linkHref = link.attr("href");//獲取href中的數據。
//截取id 拼接真實路徑
String id = subString(linkHref, "id_", ".html");
String realURL = "http://player.youku.com/embed/"+id;
System.out.println("realURL = " +realURL);
linkSB.append(realURL);
linkSB.append("\r\n");
}
toFile(linkSB.toString(), "G:\\url.txt");
}
public static void main(String[] args) {
//獲取網頁中指定標籤下的a連接
//getHTML("http://list.youku.com/albumlist/show/id_27558795.html?spm=a2h0k.8191403.0.0&sf=10101");
/*//獲取整個頁面的源碼下載。
* String urlyuan = "http://list.youku.com/albumlist/show/id_27558795.html?spm=a2h0k.8191403.0.0&sf=10101";
* String toFilePath = "G:\\indext.html"; //獲取url源
* getUrlYuan(urlyuan, toFilePath);
*/
getLocalFile("G:\\1.html");
}
}