当前位置: 首页 > 知识库问答 >
问题:

Jsoup 红滴图像刮刀 超过 18 期

彭令秋
2023-03-14

我正在开发一个图像抓取器,它使用JSOUP抓取各种子reddit的第一页。然而,出现的问题是,当试图抓取NSFW子reddit时,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();






}



}

共有1个答案

袁永贞
2023-03-14

我在这个链接中发布了一个使用Jsoup对服务器进行身份验证的答案。基本上,您需要POST您的登录ID

连接。响应res=Jsoup.connect(url).data(…).method(method.Post)。执行(),然后保存来自服务器的响应cookie,以保持会话的身份验证。

 类似资料:
  • 我在wiki上搜索目录时遇到了麻烦。我正在为一个个人项目做一个简单的网页刮刀,我不知道如何刮这些数据。 下面是我试图从任何给定的wiki页面中刮取目录的尝试 下面是我要刮的页面中的代码,我如何只得到单词“chronology”?:

  • 我有下面的代码,我正在使用从flipkart网站的图片,所有的工作都很好,但我不知道为什么它只是检索前四个图片,而不是所有的图片出现在网页上。帮助我排序这一个

  • 我正在尝试使用jSoup刮一个网站,有以下内容。我对jSoup很陌生,还在想办法。我想做的是能够采取的产品名称和价格,并把他们放入一个excel文件的名称在A列和价格在B列,0.00可以忽略或放在C列,无论什么更容易。任何帮助都将是伟大的,因为我知道有人会问,这不是一个家庭作业。 谢谢提前,我真的很感激。 **这是否是表元素,因为这是列表前的“表”代码,如果不是,我应该在html代码中查找什么?

  • 我打算创建一个Android应用程序,它可以无头登录一个网站,然后在维护登录会话的同时从后续页面中删除一些内容。 我第一次在一个普通的Java项目中使用HtmlUnit,它工作得很好。但后来发现HtmlUnit与Android不兼容。 然后我通过向登录表单发送HTTP“POST”请求来尝试JSoup库。但由于JSoup不支持JavaScript,因此生成的页面无法完全加载。 然后有人建议我看看Se

  • 我对python有点陌生,但我正在尝试制作一个web scraper脚本,它可以在网站上下载所有图片。我正在使用requests和PyQuery,因为许多人在做了一些研究后推荐了它。这就是我现在所拥有的,我不知道该去哪里。 我知道我需要获取img的来源,但在找到img标签后如何做到这一点?此外,我查看了一些htmls的页面源,一些图片存储在他们的数据库中,因此src以“/”开头一些扩展“所以我想知

  • 嗨,我终于能够设置我的webscraper,并将数据导入到我的网页中:) 但是我的网页在端口3001上运行,而网页刮刀在端口8080上运行,我有点困惑,我怎么能设置一个计时器来更新后台的刮刀? Scraper.js 弗雷德里克