0

My code pulls the links and adds them to the HashSet. I want the link to replace the original link and repeat the process till no more new links can be found to add. The program keeps running but the link isn't updating and the program gets stuck in an infinite loop doing nothing. How do I get the link to update so the program can repeat until no more links can be found?

package downloader;

import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Scanner;
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 Stage2 {
    public static void main(String[] args) throws IOException {

        int q = 0;
        int w = 0;


        HashSet<String> chapters = new HashSet();
        String seen = new String("/manga/manabi-ikiru-wa-fuufu-no-tsutome/i1778063/v1/c1");
        String source = new String("https://mangapark.net" + seen);
        //                          0123456789
       while( q == w ) {



        String source2 = new String(source.substring(21));

        String last = new String(source.substring(source.length() - 12));
        String last2 = new String(source.substring(source.length() - 1));


        chapters.add(seen);
        for (String link : findLinks(source)) {

            if(link.contains("/manga") && !link.contains(last) && link.contains("/i") && link.contains("/c") && !chapters.contains(link)) {
            chapters.add(link);
                System.out.println(link);
                seen = link;


                System.out.print(chapters);
                System.out.println(seen);
            }

            }

        }

      System.out.print(chapters);



    }

    private static Set<String> findLinks(String url) throws IOException {

        Set<String> links = new HashSet<>();

        Document doc = Jsoup.connect(url)
                .data("query", "Java")
                .userAgent("Mozilla")
                .cookie("auth", "token")
                .timeout(3000)
                .get();

        Elements elements = doc.select("a[href]");
        for (Element element : elements) {
            links.add(element.attr("href"));
        }


        return links;

    }

}

  • 1
    Regarding the infinite loop, `while( q == w )` will indeed run forever, since this statement will always be true. You never change the value of q or w. If you want it to end, you should change this, so the value returns false at some point. Or you could use a `break` statement. –  Mar 22 '20 at 03:40

1 Answers1

0

Your progamm didn't stop becouse yout while conditions never change:

while( q == w )

is always true. I run your code without the while and I got 2 links print twice(!) and the programm stop.

If you want the links to the other chapters you have the same problem like me. In the element

Element element = doc.getElementById("sel_book_1");

the links are after the pseudoelement ::before. So they will not be in your Jsoup Document.

Here is my questsion to this topic:

How can I find a HTML tag with the pseudoElement ::before in jsoup

Steffi
  • 296
  • 1
  • 2
  • 8