【问题标题】:Jsoup Reddit Image Scraper Over 18 issueJsoup Reddit Image Scraper Over 18 issue
【发布时间】:2016-02-17 18:10:30
【问题描述】:

我正在开发一个图像抓取工具,它使用 JSOUP 抓取各种 subreddit 的第一页。然而,出现的问题是,当尝试抓取 NSFW 子版块时,reddit 会重定向到超过 18 个身份验证页面,而抓取工具会抓取身份验证页面。我是新手,知道这是一个菜鸟问题,但是我完全迷路了,任何帮助都将不胜感激。

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class javascraper{
    public static final String USER_AGENT = "<User-Agent: github.com/dabeermasood:v1.2.3 (by /u/swedenotswiss)>";


public static void main (String[]args) throws MalformedURLException
{
    Scanner scan = new Scanner (System.in);
    System.out.println("Where do you want to store the files?");
    String folderpath = scan.next();
    System.out.println("What subreddit do you want to scrape?");
    String subreddit = scan.next();
    subreddit = ("http://reddit.com/r/" + subreddit);

    new File(folderpath + "/" + subreddit).mkdir();


//test

try{
    //gets http protocol
    Document doc = Jsoup.connect(subreddit).userAgent(USER_AGENT).timeout(0).get();

//get page title
String title = doc.title();
System.out.println("title : " + title);

//get all links
Elements links = doc.select("a[href]");

for(Element link : links){

//get value from href attribute
String checkLink = link.attr("href");
Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
if (imgCheck(checkLink)){ // checks to see if img link j
    System.out.println("link : " + link.attr("href"));
downloadImages(checkLink, folderpath);





}
}



}
catch (IOException e){
e.printStackTrace();
}

}


public static boolean imgCheck(String http){
String png = ".png";
String jpg = ".jpg";
String jpeg = "jpeg"; // no period so checker will only check last four characaters
String gif = ".gif";
int length = http.length();

if (http.contains(png)|| http.contains("gfycat") || http.contains(jpg)|| http.contains(jpeg) || http.contains(gif)){
return true;
}
else{
return false;
}



}



private static void downloadImages(String src, String folderpath) throws IOException{
String folder = null;

        //Exctract the name of the image from the src attribute

        int indexname = src.lastIndexOf("/");

        if (indexname == src.length()) {

            src = src.substring(1, indexname);

        }
 indexname = src.lastIndexOf("/");

        String name = src.substring(indexname, src.length());

        System.out.println(name);

        //Open a URL Stream

        URLConnection connection = (new URL(src)).openConnection();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {

            e.printStackTrace();
        } //Delay to comply with rate limiting
        connection.setRequestProperty("User-Agent", USER_AGENT);

        InputStream in = connection.getInputStream();

        OutputStream out = new BufferedOutputStream(new FileOutputStream( folderpath+ name));



        for (int b; (b = in.read()) != -1;) {

            out.write(b);

        }

        out.close();

        in.close();






}



}

【问题讨论】:

  • 您必须处理并提交身份验证页面,然后抓取以下页面。

标签: java web-scraping jsoup reddit


【解决方案1】:

我已经在this link 中发布了使用Jsoup 对服务器进行身份验证的答案。基本上你需要POST你的登录ID和密码以及其他所需的数据到服务器使用:

Connection.Response res = Jsoup.connect(url).data(...).method(Method.Post).execute();,然后保存来自服务器的响应 cookie 以保持您的会话通过身份验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多