一個簡單java爬蟲知乎某個話題下的照片

來源:互聯網
上載者:User
package Util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.HttpURLConnection;import java.net.URL;import java.util.regex.Matcher;import java.util.regex.Pattern;public class UrlGet {    public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        //要爬蟲網頁的url        String url="https://www.zhihu.com/question/38376393";        //返回儲存有效url的檔案夾名稱        String urlfilename = GetUrl(url);        //下載有效url所對應的照片        downPic(urlfilename);        System.out.println("Picture download completed");    }    //從已經下載下來的有效url中    public static void downPic(String urlfilename) throws Exception {        // TODO Auto-generated method stub//      URL url = new URL(null);        BufferedReader  bfr=                new BufferedReader(new FileReader(urlfilename));        String url1 = null;        int num=0;        while((url1=bfr.readLine())!=null){            num++;            downPic(bfr, url1,num);        }        bfr.close();    }    /**     * 通過擷取的url下載圖片*/    public static void downPic(BufferedReader bfr, String url1 , int num)throws IOException {        URL url = new URL(url1);        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();        //擷取下載輸入資料流        BufferedInputStream  bis=                new BufferedInputStream(urlConnection.getInputStream());        //寫出到downPic檔案夾中的流        BufferedOutputStream bos =                new BufferedOutputStream(new FileOutputStream(new File("F:/downPic1/"+num+".jpg")));        int ch=0;        byte[] buf = new byte[1024];        //寫入到downPic        while((ch=bis.read(buf))!=-1){            bos.write(buf,0,ch);        }        bos.close();    }    //擷取網頁有效UrL    public static String GetUrl(String url) throws Exception {        // TODO Auto-generated method stub        URL u=new URL(url);        HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection();        BufferedReader  bus=                new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));        PrintWriter pw = new PrintWriter(new FileWriter("F:/zhihu_pic_url.txt"),true);        PrintWriter pw1 = new PrintWriter(new FileWriter("F:/zhihu_content.txt"),true);        String regex= "[https|http]+://+[pic]+[A-Za-z0-9.//_%=-]*";        Pattern p = Pattern.compile(regex);        String line = null;        while((line=bus.readLine())!=null){            Matcher m = p.matcher(line);            pw1.println(line);            while(m.find()){                pw.println(m.group());            }        }        bus.close();        pw.close();        pw1.close();        System.out.println("Urlget completed");        String urlfilename="F:/zhihu_pic_url.txt";        return urlfilename;    }}

此方法只適用知乎,因為其中的Regex只按照知乎網頁原始碼,不過擷取的網址還是有重複的,只是大小,解析度不同,運行結果如下

這是擷取的有用url,由此得到的照片如下

從上面可以看到下載的照片是有重複的,仔細對比url可以看出,只是照片的大小,解析度不同,所以還可以再進行改進Regex。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.