生活随笔
收集整理的這篇文章主要介紹了
java爬虫下载付费html网页模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
前一段時間我們有一個網頁的projiect小項目,要求學習bootstarp。然而自己寫的模板和別人寫好的東西,無論從美觀和手機運行的兼容性上差距都很巨大。中途我們放棄自己寫的東西,開始偷別人的模板。有些甚至不會偷的同學甚至還付費下載,都什么年代了,程序員還要花錢買模板。那次結束后,突發奇想能不能寫個程序,讓他自動下載模板。經過不斷努力和解決bug,最終取得了成功。
思路
大致思路為:輸入模板的一個頁面為url,通過這個鏈接遍歷所有與之有關的鏈接放到hashset中(采用隊列的寬度優先遍歷bfs)。這個相關用字符判斷鏈接前面的主要域名地址。(鏈出去的鏈接不處理,防止無限擴大)。同時,還要將各種url分類放到不同的set中。
html頁面分析:抓取html鏈接。還要按行讀取html文本分析其中可能隱藏的css文件(可能有背景圖片)。獲取js鏈接,獲取image地址,css地址,(注意一定要儲存絕對地址而不是相對地址)。還有的涉及到上層目錄。需要處理。
css頁面:按行分析。因為css中可能儲存背景圖片以及其他logo。
js:直接下載保存。
html:下載保存
image:下載保存
注意點:
所有下載鏈接或者其他活動都要在try catch進行,在catch中跳過這個步驟,執行相應步驟。下載目錄在download自行更改(默認F://download)添加jsoup的jar包有些圖片藏在js文件中和css文件中,所以需要去判斷js文件和css文件,我這個只分析了css沒分析css。由于精力和時間問題,項目并沒有晚上,由于筆者此時正則能力不足,大部分采用字符串分割查找或者contains查找,難免有疏漏目前代碼測試只針對17素材之家部分模板測試有效。其他站點未進行測試只是小白,代碼亢長低水平,大佬勿噴。
附上代碼如下:
代碼
import java
.io
.IOException
;
import java
.util
.Iterator
;
import java
.util
.Scanner
;
import java
.util
.concurrent
.ExecutorService
;
import java
.util
.concurrent
.Executors
;public class getmoban {public static void main(String
[] args
) throws IOException
{ExecutorService ex
=Executors
.newFixedThreadPool(6);Scanner sc
=new Scanner(System
.in
);System
.out
.println("請輸入網址(別太大否則下載不完)");String url
=sc
.nextLine();geturl g
=new geturl(url
);csssearch cssimage
=new csssearch();System
.out
.println(g
.file
);g
.judel(); Iterator it
=g
.htmlurlset
.iterator(); while(it
.hasNext()){String name
=it
.next();try {download download
=new download(name
);ex
.execute(download
); }catch(Exception e
){}}Iterator it2
=g
.jsset
.iterator();while(it2
.hasNext()){String name
=it2
.next();try {download download
=new download(name
);ex
.execute(download
); }catch(Exception e
){}}Iterator it3
=g
.cssset
.iterator();while(it3
.hasNext()){String name
=it3
.next();try {download download
=new download(name
);ex
.execute(download
);cssimage
.searchimage(name
);}catch(Exception e
){}}Iterator it4
=g
.imgset
.iterator();while(it4
.hasNext()){String name
=it4
.next();try {download download
=new download(name
);ex
.execute(download
); }catch(Exception e
){}}ex
.shutdown();}
}
import java
.io
.IOException
;
import java
.util
.ArrayDeque
;
import java
.util
.HashSet
;
import java
.util
.Iterator
;
import java
.util
.Queue
;
import java
.util
.Set
;import org
.jsoup
.Jsoup
;
import org
.jsoup
.nodes
.Document
;
import org
.jsoup
.nodes
.Element
;
import org
.jsoup
.select
.Elements
;public class geturl {public static String url
="http://www.17sucai.com/preview/1/2014-11-28/jQuery用戶注冊表單驗證代碼/index.html";static String head
="http";public geturl(String url
){this.url
=url
;}static String file
=url
;{if(url
.contains("http")){head
=file
.split("//")[0];file
=file
.split("//")[1];}int last
=file
.lastIndexOf("/");file
=file
.substring(0, last
);}static Set htmlurlset
=new HashSet();static Set jsset
=new HashSet();static Set imgset
=new HashSet();static Set cssset
=new HashSet();static Queue queue
=new ArrayDeque();
public static void judel() throws IOException
{queue
.add(url
);htmlurlset
.add(url
);while(!queue
.isEmpty()&&queue
!=null
){String teamurl
=queue
.poll();System
.out
.println(teamurl
);if(!teamurl
.endsWith(".com")){if(file
.indexOf("/")>0){if(teamurl
.contains(file
.substring(0,file
.indexOf("/"))))analyze(teamurl
);}elseanalyze(teamurl
);}
}}public static void analyze(String URL
){try {Document doc
;doc
= Jsoup
.connect(URL
).timeout(20000).header("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36").ignoreContentType(true).get();Elements all
=doc
.select("[class]");Elements js
=doc
.getElementsByTag("script");Elements html
=doc
.select("a[href]");Elements img
=doc
.select("img");Elements css
=doc
.select("link[href]");for(Element e
:all
){if(e
.attr("style")!=""){ String tex
=e
.attr("style");if(tex
.contains("url")){String urladress
=file
;String imgurl
=tex
.split("url")[1];imgurl
=imgurl
.split("\\(")[1].split("\\)")[0];if(imgurl
.startsWith("'")||imgurl
.startsWith("\"")){imgurl
=imgurl
.substring(1,imgurl
.length()-1);} while(imgurl
.startsWith("..")){imgurl
=imgurl
.substring(imgurl
.indexOf("/") 1); urladress
=urladress
.substring(0,urladress
.lastIndexOf("/"));}urladress
=head
"//" urladress
"/" imgurl
;imgset
.add(urladress
);} }}for(Element htmlelement
:html
){ String a
=htmlelement
.absUrl("href").split("#")[0];if(!a
.equals("")){if(!htmlurlset
.contains(a
)&&a
.contains(file
.substring(0,file
.indexOf("/")))){ queue
.add(a
);htmlurlset
.add(a
); } } }for(Element jselement
:js
){String team
=jselement
.absUrl("src"); if(!team
.equals(""))jsset
.add(team
);}for(Element csselement
:css
){String team
=csselement
.absUrl("href");if(!team
.equals(""))cssset
.add(team
); }for(Element imageelement
:img
){String team
=imageelement
.absUrl("src");if(!team
.equals(""))imgset
.add(team
);}}catch(Exception e
){if(!queue
.isEmpty()) {URL
=queue
.poll();analyze(URL
);}}} }
- 分析css(css可能隱藏圖片)csssearch
import java
.io
.BufferedReader
;
import java
.io
.File
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.io
.InputStreamReader
;
import java
.net
.URL
;
import java
.net
.URLConnection
;
import java
.util
.HashSet
;
import java
.util
.Iterator
;
import java
.util
.Set
;public class csssearch {public static void searchimage(String ur
) throws IOException
{if(ur
.toLowerCase().contains("bootstarp")) {return;}Set imgset
=new HashSet();String http
="http";String fileurl
=ur
;if(fileurl
.startsWith("http")){http
=fileurl
.split("//")[0];fileurl
=fileurl
.split("//")[1];}fileurl
=fileurl
.substring(0,fileurl
.lastIndexOf("/"));URL url
=new URL(ur
);URLConnection conn
= url
.openConnection();conn
.setConnectTimeout(1000);conn
.setReadTimeout(5000);conn
.connect();InputStream in
= conn
.getInputStream();InputStreamReader inp
=new InputStreamReader(in
);BufferedReader buf
=new BufferedReader(inp
);File file
=new File("F:\\download\\" ur
.split("//")[1]);if(!file
.exists()){file
.getParentFile().mkdirs();file
.createNewFile();}String tex
="";while((tex
=buf
.readLine())!=null
){
if(tex
.contains("url")){String urladress
=fileurl
;String imgurl
=tex
.split("url")[1];imgurl
=imgurl
.split("\\(")[1].split("\\)")[0];if(imgurl
.startsWith("'")||imgurl
.startsWith("\"")){imgurl
=imgurl
.substring(1,imgurl
.length()-1);}while(imgurl
.startsWith("..")){imgurl
=imgurl
.substring(imgurl
.indexOf("/") 1); urladress
=urladress
.substring(0,urladress
.lastIndexOf("/"));}urladress
=http
"//" urladress
"/" imgurl
;imgset
.add(urladress
);}}buf
.close();inp
.close();in
.close();Iterator it
=imgset
.iterator();while(it
.hasNext()){ String team
=it
.next();try {download down
=new download(team
);Thread t1
=new Thread(down
);t1
.start();System
.out
.println(team
"下載成功");}catch(Exception e
) {System
.out
.println("下載失敗:" team
);}}}
}
import java
.io
.BufferedInputStream
;
import java
.io
.BufferedOutputStream
;
import java
.io
.File
;
import java
.io
.FileOutputStream
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.net
.URL
;
import java
.net
.URLConnection
;public class download implements Runnable{public String ur
;public download() {}public download(String ur
){this.ur
=ur
;}public static void download(String ur
) throws IOException
{ String fileplace
=ur
;if(fileplace
.contains("http")){fileplace
=fileplace
.split("//")[1];}URL url
= new URL(ur
);URLConnection conn
= url
.openConnection();conn
.setConnectTimeout(4000);conn
.setReadTimeout(5000);conn
.connect();InputStream in
= conn
.getInputStream();BufferedInputStream buf
=new BufferedInputStream(in
);File file
=new File("F:\\download\\" fileplace
);if(!file
.exists()){file
.getParentFile().mkdirs();file
.createNewFile();}BufferedOutputStream bufout
=new BufferedOutputStream(new FileOutputStream(file
));
byte b
[]=new byte[1024];int n
=0;while((n
=buf
.read(b
))!=-1){bufout
.write(b
, 0, n
);}in
.close();buf
.close(); bufout
.close();}@Overridepublic void run() {try {download(ur
);System
.out
.println(Thread
.currentThread().getName() " 下載" ur
"成功");} catch (IOException e
) {e
.printStackTrace();}}}
目標網站,以及運行效果
還有注意的要進入初始界面,把上面的X 點進去。
只能下載17素材大部分模板。如果以后時間充足,可能會維護下,下載更多模板!
項目github地址
如果對后端、爬蟲、數據結構算法等感性趣歡迎關注我的個人公眾號交流:bigsai
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的java爬虫下载付费html网页模板的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。