简体   繁体   English

如何保持此代码重复多次

[英]How to keep this code repeating more than once

My code pulls the links and adds them to the HashSet.我的代码提取链接并将它们添加到 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;

    }

}

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.我在没有 while 的情况下运行你的代码,我得到了 2 个链接打印两次(!)并且程序停止。

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.链接在伪元素 ::before 之后。 So they will not be in your Jsoup Document.所以它们不会出现在你的 Jsoup 文档中。

Here is my questsion to this topic:这是我对这个主题的问题:

How can I find a HTML tag with the pseudoElement ::before in jsoup 如何在jsoup中找到带有pseudoElement ::before的HTML标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM