Java 如何找到与Jsoup匹配的所有锚?
提前感谢您抽出时间。代码应该连接到网站,并从包含用户输入的单词的行中删除操作系统模型。它将搜索该单词,转到该行,并从该行的OS属性中删除该单词。我不明白为什么我的代码不起作用,希望能得到一些帮助 这是网站 代码如下:Java 如何找到与Jsoup匹配的所有锚?,java,html,web-scraping,jsoup,Java,Html,Web Scraping,Jsoup,提前感谢您抽出时间。代码应该连接到网站,并从包含用户输入的单词的行中删除操作系统模型。它将搜索该单词,转到该行,并从该行的OS属性中删除该单词。我不明白为什么我的代码不起作用,希望能得到一些帮助 这是网站 代码如下: import java.io.IOException; import java.util.Iterator; import java.util.Scanner; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; im
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ExtraPart1 {
public static void main(String args[]) throws IOException{
Scanner input = new Scanner(System.in);
String word = "";
System.out.println("Type in what you are trying to search for.");
word = input.nextLine();
System.out.println("This program will find a quality from a website for it");
String URL = "http://www.tabletpccomparison.net/";
Document doc = Jsoup.connect(URL).get();
Elements elements = doc.select("a");
for(Element e : elements){
if(e.equals(word)){
String next_word = e.getElementsByClass("tableJX2ope_sis").text();
System.out.print(next_word);
}
}
}
}
问题在于:
if(e.equals(word)){
String next_word = e.getElementsByClass("tableJX2ope_sis").text();
System.out.print(next_word);
}
e
是一个元素
,它与字符串
相比较。请尝试以下方法:
if(e.text().equals(word)) {
// ...
}
您可以简化for循环,如下所示:
String cssQuery = String.format("a:containsOwn(%s)", word);
Elements elements = doc.select(cssQuery);
for(Element e : elements){
String nextWord = e.getElementsByClass("tableJX2ope_sis").text();
System.out.print(nextWord);
}
工具书类
表中定位要刮取的链接。通过选择ononlya
,您必须迭代文档上的每个链接
String selector = String.format(
"table.tableJX tr:contains(%s) > td.tableJX2ope_sis > span.field", word);
for (Element os : doc.select(selector))
System.out.println(os.ownText());