1. 程式人生 > >[Java] 【分享】Java爬蟲

[Java] 【分享】Java爬蟲

Java爬蟲合集


剛剛學習爬蟲,把自己學習寫的程式碼分享一下,都放在一個專案裡面了,每個包裡都是不同的爬蟲小demo



爬取的內容:
                           嘀哩嘀哩網站圖片的
                           遊民星空相簿桌布
                           ACG桌布
                           爬取QQ音樂
                           還有美女寫真
                              
                       介面就隨便寫下。。。。



                      在貼一點程式碼

// 解析網頁
        public void ParseHtml(int sin,List<String> pagelinks,File dirs) throws IOException {
                // 遍歷每個頁碼的連結
                for (String pagelink : pagelinks) {
                        Document doc = Jsoup.connect(pagelink)
                                        .header("User-Agent",
                                                        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
                                        .get();
                        // 得到需要的條目的連結
                        Elements itemlinks = doc.select("tr td h3 a[href^=htm]");
                        // 遍歷每個條目
                        for (Element itemlink : itemlinks) {
                                // 建立執行緒 重寫run方法 進入每個條目的連結
                                Thread t = new Thread() {
                                        @Override
                                        public void run() {
                                                try {
                                                        String hrefs = itemlink.absUrl("href");
                                                        Document imgdoc = Jsoup.connect(hrefs)
                                                                        .header("User-Agent",
                                                                                        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
                                                                        .get();
                                                        // 根據網頁情況獲得h1標籤
                                                        Element h1;
                                                        h1 = imgdoc.selectFirst("div h1");
                                                        if (h1 == null)
                                                                h1 = imgdoc.selectFirst("h1#subject_tpc");
                                                        // 得到圖片或小說或電影的標題
                                                        String name = h1.text();
                                                        // 得到圖片
                                                        Elements imgs = imgdoc.select("div.tpc_content img");
                                                        // 得到下載連結
                                                        Element links = imgdoc
                                                                        .selectFirst("div.tpc_content a[href^=http://www3.uptorrentfilespace]");
                                                        // 得到小說正文
                                                        Element body ;
                                                         
                                                        if (sin == 1) {
                                                                // 用來儲存圖片連結
                                                                ArrayList<String> arr = new ArrayList<String>();
                                                                /*
                                                                 * 判斷進入不同的儲存檔案的方法 1.如果圖片和連結都不為空,說明是電影
                                                                 * 2.如果1的條件不通過,判斷圖片是否為空,如果不為空,說明是圖片
                                                                 * 
                                                                 */
 
                                                                if (imgs != null && links != null) {
                                                                        for (Element img : imgs) {
                                                                                String link = img.absUrl("src");
                                                                                arr.add(link);
                                                                                System.out.println("獲得第" + i++ + "條連結-----" + link);
                                                                        }
                                                                        String text = links.text();
                                                                        download(name, arr, text,dirs);
                                                                } else {
                                                                        for (Element img : imgs) {
                                                                                String link = img.absUrl("src");
                                                                                arr.add(link);
                                                                                System.out.println("獲得第" + i++ + "條連結-----" + link);
                                                                        }
                                                                        download(name, arr,dirs);
                                                                }
                                                        }
                                                        if (sin == 2) {
                                                                body = imgdoc.selectFirst("div.tpc_content");
                                                                // 去除小說中的<br>標籤,並加上換行
                                                                String text = body.toString().replaceAll("<br>", "\\n");
                                                                // 開執行緒去儲存小說
                                                                new Thread() {
                                                                        public void run() {
                                                                                try {
                                                                                        File dir = new File("D://download1//小說//" + name);
                                                                                        if (!dir.exists())
                                                                                                dir.mkdirs();
                                                                                        BufferedOutputStream bos = new BufferedOutputStream(
                                                                                                        new FileOutputStream(new File(dir, "小說.txt")));
                                                                                        bos.write(text.getBytes());
                                                                                        bos.close();
                                                                                        System.out.println("下載成功 : " + dir);
                                                                                } catch (Exception e) {
                                                                                        // TODO Auto-generated catch block
                                                                                        //e.printStackTrace();
                                                                                }
                                                                        };
                                                                }.start();
                                                        }
                                                } catch (Exception e) {
                                                        // TODO Auto-generated catch block
                                                        //e.printStackTrace();
                                                }
                                        }
                                };
                                // 通過執行緒池動態控制 執行建立的執行緒
                                service.execute(t);
                        }
                }
其他的程式碼下載專案看吧
其實裡面是有福利的   但是要自己發掘

連結:https://pan.baidu.com/s/1zW1wRMyP2etiLvfnfASIgw 密碼:8ent



 

捕獲2.PNG (14.23 KB, 下載次數: 4)

捕獲2.PNG