您好,登錄后才能下訂單哦!
大多數情況下,我們要獲取頁面源碼等html代碼中的某個標簽的某個屬性值時,不可能大費周章的去使用第三方框架,這就有點牛刀殺雞的趕腳了。遇到這種情況我們可以使用簡單的正則表達式來提取我們想要的數據。
例如,我要下個連續劇,大多數網站提供給我們的是很多列表,需要我們去一個一個去點擊,這就很麻煩,現在,只需要我們使用http獲取到頁面的html字符串,然后使用正則表達式批量獲取到a標簽的href屬性的值就可以了。
現在我們以某電影網站某雷下載為例,現在demo如下:
工具方法httpSendGet:
public static String httpSendGet(String url, String param,String charsetName) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實際的連接
connection.connect();
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), charsetName));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送GET請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
工具方法match:
public static List<String> match(String source, String element, String attr) {
List<String> result = new ArrayList<String>();
String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
Matcher m = Pattern.compile(reg).matcher(source);
while (m.find()) {
String r = m.group(1);
result.add(r);
}
return result;
}
調用demo:
public static void main(String[] args) {
String url = "https://www.dy2018.com/i/99671.html";
String params = "";
String html = httpSendGet(url,params,"gb2312");
List<String> links = match(html,"a","href");
System.out.println(links);
}
這里需要說明一下,httpSendGet的charsetName參數需要注意,不然你獲取的html文本會是亂碼。
最后展示一下結果(當然結果還不純粹,需要過濾):
[/, /2/, /0/, /3/, /1/, /4/, /8/, /5/, /7/, /14/, /15/, /html/tv/hytv/index.html, /html/tv/oumeitv/index.html, /html/tv/rihantv/index.html, /html/zongyi2013/index.html, /html/dongman/index.html, /support/GuestBook.php, #, index.html, /, /html/tv/, /html/tv/hytv/, javascript:window.external.addFavorite('http://www.dy2018.com/','dy2018.com-電影天堂')"class="style11, /webPlay/play-id-99671-collection-37.html, /webPlay/play-id-99671-collection-36.html, /webPlay/play-id-99671-collection-35.html, /webPlay/play-id-99671-collection-34.html, /webPlay/play-id-99671-collection-33.html, /webPlay/play-id-99671-collection-32.html, /webPlay/play-id-99671-collection-31.html, /webPlay/play-id-99671-collection-30.html, /webPlay/play-id-99671-collection-29.html, /webPlay/play-id-99671-collection-28.html, /webPlay/play-id-99671-collection-27.html, /webPlay/play-id-99671-collection-26.html, /webPlay/play-id-99671-collection-25.html, /webPlay/play-id-99671-collection-24.html, /webPlay/play-id-99671-collection-23.html, /webPlay/play-id-99671-collection-22.html, /webPlay/play-id-99671-collection-21.html, /webPlay/play-id-99671-collection-20.html, /webPlay/play-id-99671-collection-19.html, /webPlay/play-id-99671-collection-18.html, /webPlay/play-id-99671-collection-17.html, /webPlay/play-id-99671-collection-16.html, /webPlay/play-id-99671-collection-15.html, /webPlay/play-id-99671-collection-14.html, /webPlay/play-id-99671-collection-13.html, /webPlay/play-id-99671-collection-12.html, /webPlay/play-id-99671-collection-11.html, /webPlay/play-id-99671-collection-10.html, /webPlay/play-id-99671-collection-9.html, /webPlay/play-id-99671-collection-8.html, /webPlay/play-id-99671-collection-7.html, /webPlay/play-id-99671-collection-6.html, /webPlay/play-id-99671-collection-5.html, /webPlay/play-id-99671-collection-4.html, /webPlay/play-id-99671-collection-3.html, /webPlay/play-id-99671-collection-2.html, /webPlay/play-id-99671-collection-1.html, /webPlay/play-id-99671-collection-0.html, ftp://g:g@tv.kaida365.com:2166/一千零一夜35.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜34.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜33.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜32.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜31.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜30.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜29.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜28.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜27.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜26.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜25.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜24.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜23.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜22.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜21.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜20.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜19.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜18.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜17.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜16.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜15.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜14.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜13.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜12.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜11.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜10.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜09.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜08.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜07.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜06.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜05.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜04.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜03.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜02.mp4, ftp://g:g@tv.kaida365.com:2166/一千零一夜01.mp4, /i/99743.html, /i/99734.html, /i/99733.html, /i/99725.html, /i/99720.html, /i/99719.html, /i/99716.html, /i/99708.html, /i/99704.html, /i/99695.html, /i/97129.html, /i/97575.html, /i/97041.html, /i/92091.html, /i/97637.html, /i/92020.html, /i/95187.html, /i/92000.html, /i/98343.html, /i/97363.html]
不想自己寫正則表達式,可以使用第三方爬蟲框架,這方面網上找很多的,我就不寫了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。