在以往用java來處理解析html文檔或者片段時,我們通常會采用htmlparser(http://htmlparser.sourceforge.net/)這個開源類庫。現在我們有了jsoup,以后的處理html的內容只需要使用jsoup就已經足夠了,jsoup有更快的更新,更方便的api等。
jsoup 是一款 java 的html 解析器,可直接解析某個url地址、html文本內容。它提供了一套非常省力的api,可通過dom,css以及類似于jquery的操作方法來取出和操作數據,可以看作是java版的jquery。
jsoup的主要功能如下:
- 從一個url,文件或字符串中解析html;
- 使用dom或css選擇器來查找、取出數據;
- 可操作html元素、屬性、文本;
jsoup是基于mit協議發布的,可放心使用于商業項目。官方網站:http://jsoup.org/
步驟大致可以分為三個模塊:一是獲取網頁的資源,二是解析獲取的資源,取出我們想要的圖片url地址,三是通過java的io存儲在本地文件中。
獲取網頁資源的核心模塊就是通過jsoup去獲取網頁的內容,具體核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private static list<jsoupimagevo> findimagenourl(string hotelid, string url, int timeout) { list<jsoupimagevo> result = new arraylist<jsoupimagevo>(); document document = null ; try { document = jsoup.connect(url).data( "query" , "java" ) //請求參數 .useragent( "mozilla/4.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0)" ) //設置urer-agent get(); .timeout(timeout) .get(); string xmlsource = document.tostring(); result = dealresult(xmlsource, hotelid); } catch (exception e) { string defaulturl = "http://qnimg.zowoyoo.com/img/15463/1509533934407.jpg" ; result = dealresult(defaulturl,hotelid); } return result; } |
其中url地址是百度圖片搜索的地址,具體調用代碼如下:
1
2
3
4
5
6
|
public static list<jsoupimagevo> findimage(string hotelname, string hotelid, int page) { int number= 5 ; string url = "http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=" + hotelname + "&cg=star&pn=" + page * 30 + "&rn=" +number+ "&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=" + integer.tohexstring(page * 30 ); int timeout = 5000 ; return findimagenourl(hotelid, url, timeout); } |
這里需要注意的是:word是我們要搜索的關鍵字,pn是顯示的頁碼,rn是一頁顯示多少個數據。
解析網頁的資源,然后封裝起來。核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private static list<jsoupimagevo> dealresult(string xmlsource, string hotelid) { list<jsoupimagevo> result = new arraylist<jsoupimagevo>(); xmlsource = stringescapeutils.unescapehtml3(xmlsource); string reg = "objurl\":\"http://.+?\\.(gif|jpeg|png|jpg|bmp)" ; pattern pattern = pattern.compile(reg); matcher m = pattern.matcher(xmlsource); while (m.find()) { jsoupimagevo jsoupimagevo = new jsoupimagevo(); string imageurl = m.group().substring( 9 ); if (imageurl== null || "" .equals(imageurl)){ string defaulturl = "http://qnimg.zowoyoo.com/img/15463/1509533934407.jpg" ; jsoupimagevo.seturl(defaulturl); } else { jsoupimagevo.seturl(imageurl); } jsoupimagevo.setname(hotelid); result.add(jsoupimagevo); } return result; } |
這里最主要的地方就是reg這個正則表達式,通過正則表達式,去網頁中解析符合規定的圖片url地址,然后封裝在對象中。
最后一部分就是通過java的io流去圖片地址獲取圖片,并保存在本地。核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//根據圖片網絡地址下載圖片 public static void download(string url,string name,string path){ file file= null ; file dirfile= null ; fileoutputstream fos= null ; httpurlconnection httpcon = null ; urlconnection con = null ; url urlobj= null ; inputstream in = null ; byte [] size = new byte [ 1024 ]; int num= 0 ; try { dirfile = new file(path); if (dirfile.exists()){ dirfile.delete(); } dirfile.mkdir(); file = new file(path+ "//" +name+ ".jpg" ); fos = new fileoutputstream(file); if (url.startswith( "http" )){ urlobj = new url(url); con = urlobj.openconnection(); httpcon =(httpurlconnection) con; in = httpcon.getinputstream(); while ((num=in.read(size)) != - 1 ){ for ( int i= 0 ;i<num;i++) fos.write(size[i]); } } } catch (filenotfoundexception notfounde) { logutils.writelog( "找不到該網絡圖片...." ); } catch (nullpointerexception nullpointere){ logutils.writelog( "找不到該網絡圖片...." ); } catch (ioexception ioe){ logutils.writelog( "產生io異常....." ); } catch (exception e) { e.printstacktrace(); } finally { try { fos.close(); } catch (exception e) { e.printstacktrace(); } } } |
這里面的操作都是java中io篇一些基礎的操作,有不懂的可以去看看java中io模塊的內容。
因為我這邊是maven項目,所以在開發前需要引入jsoup依賴才可以。
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/hj7jay/article/details/84335161