gpt4 book ai didi

java - 多个引用和重复结果的相同 div (Jsoup)

转载 作者:行者123 更新时间:2023-12-01 11:25:58 24 4
gpt4 key购买 nike

今天遇到问题,使用JSoup库。该网站包含所需的数据,但有相同的 div,没有类,但带有“样式”。需要获取宽度的数字。

<div style="height: 12px; width: 196px; background-color: #5C1; float: left; border-right: 1px solid #111;"></div>

有两种情况: 1. 文本出现,但只有一个div; 2. 文本出现在所有div上,但出现大量重复,被视为换行。

try{
Document doc = Jsoup.connect("http://www.lolking.net/summoner/euw/34201718").get(); //Random player
Elements elem = doc.getElementsByTag("div");
Scanner scn = new Scanner(elem.toString());
while(scn.hasNext()){
String res = scn.nextLine();
if(res.contains("<div style=\"height: 12px; width: ") && res.contains("px; background-color: #5C1; float: left; border-right: 1px solid #111;\"></div>")){
if(sd == 0){ //So flooding was not. I understand that this can be a problem, but if you remove, there will be a flood of other numbers (duplicates) that cannot be removed, because each line of the program perceives as one
String t1 = res.replace(" <div style=\"height: 12px; width: ", "");
String t2 = t1.replace("px; background-color: #5C1; float: left; border-right: 1px solid #111;\"></div> ", "");
System.out.println(t2); // Get 192... and all
sd += 1;
}
}
}
}
catch(IOException e){
e.printStackTrace();
}

一整天都想不出解决办法,经历了很多,但总是来到这两种情况。最近开始学习Java。谢谢。

最佳答案

如果我理解正确的话,您需要 div 标记中的 style 内的所有 width

你当然可以使用Jsoup来做到这一点:

编辑后的代码:

    Set<String> known=new HashSet<String>();
known.add("height: 12px");
known.add("background-color: #5C1");
known.add("float: left");
known.add("border-right: 1px solid #111");
Document doc = Jsoup.connect("http://www.lolking.net/summoner/euw/34201718").get();
Elements elements=doc.select("div");
for(Element e : elements){
if(e.hasAttr("style")){
Set<String> splitted=new HashSet<String>();
for(String s : Arrays.asList(e.attr("style").split(";"))){
splitted.add(s.trim());
}
if(splitted.containsAll(known)){
splitted.removeAll(known);
for(String s: splitted){
if(s.startsWith("width:")){
System.out.println(s);
}
}
}
}
}

关于java - 多个引用和重复结果的相同 div (Jsoup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com