Java 如何使此代码重复多次
我的代码提取链接并将它们添加到哈希集中。我想要的链接,以取代原来的链接和重复的过程,直到没有更多的新链接可以找到添加。程序继续运行,但链接没有更新,程序陷入无限循环,什么也不做。如何更新链接,使程序可以重复,直到找不到更多链接Java 如何使此代码重复多次,java,replace,while-loop,jsoup,hashset,Java,Replace,While Loop,Jsoup,Hashset,我的代码提取链接并将它们添加到哈希集中。我想要的链接,以取代原来的链接和重复的过程,直到没有更多的新链接可以找到添加。程序继续运行,但链接没有更新,程序陷入无限循环,什么也不做。如何更新链接,使程序可以重复,直到找不到更多链接 package downloader; import java.io.IOException; import java.net.URL; import java.util.HashSet; import java.util.Scanner; import java.ut
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;
}
}
包下载器;
导入java.io.IOException;
导入java.net.URL;
导入java.util.HashSet;
导入java.util.Scanner;
导入java.util.Set;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
公共类阶段2{
公共静态void main(字符串[]args)引发IOException{
int q=0;
int w=0;
HashSet章=新HashSet();
所见字符串=新字符串(“/manga/manabi ikiru wa fuufu no tsutome/i1778063/v1/c1”);
字符串源=新字符串(“https://mangapark.net“+见);
// 0123456789
while(q==w){
stringsource2=新字符串(source.substring(21));
最后一个字符串=新字符串(source.substring(source.length()-12));
String last2=新字符串(source.substring(source.length()-1));
第章.添加(见);
for(字符串链接:FindLink(源)){
if(link.contains(“/manga”)&&&!link.contains(last)&&link.contains(“/i”)&&link.contains(“/c”)&&&!chapters.contains(link)){
增加(链接);
System.out.println(链接);
SEED=链接;
系统输出打印(章节);
系统输出打印项次(见);
}
}
}
系统输出打印(章节);
}
私有静态集findLinks(字符串url)引发IOException{
Set links=newhashset();
Document doc=Jsoup.connect(url)
.data(“查询”、“Java”)
.userAgent(“Mozilla”)
.cookie(“身份验证”、“令牌”)
.超时(3000)
.get();
Elements=doc.select(“a[href]”);
for(元素:元素){
links.add(element.attr(“href”);
}
返回链接;
}
}
您的程序并没有因为您而停止,而条件从未改变:
while( q == w )
这永远是真的。我运行你的代码没有时间,我得到2个链接打印两次(!)和程序停止
如果你想要其他章节的链接,你和我一样有同样的问题。在元素中
Element element = doc.getElementById("sel_book_1");
链接位于伪元素::before之后。所以它们不会出现在您的Jsoup文档中
以下是我对这个话题的提问:
关于无限循环,
while(q==w)
确实会永远运行,因为这句话永远是真的。你永远不会改变q或w的值。如果希望结束,则应更改此值,以便该值在某个点返回false。或者可以使用break
语句。