我正在通过 jsoup 开发爬虫。我想显示亚洲网上商店 https://world.taobao.com/ 的类别链接。我的代码能够找到页面上的所有链接,这要归功于:
Elements links = doc.select("a[href]");
System.out.println("Total results: " + links.size());
但不是全部。我只需要显示类别的链接,这些链接嵌套在许多 <div>
中标签。
这是我的代码:
package jsoup;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Crawler {
public static final String CLS_NAME = "Crawler";
public static final String URL_SOURCE = "https://world.taobao.com/";
public static void main(String[] args) throws IOException{
// load Document
Document doc = Jsoup.connect(URL_SOURCE).get();
// select only <a> tag with "href" attribute
Elements links = doc.select("a[href]");
System.out.println("Total results: " + links.size());
for (Element url: links){
System.out.println(String.format("* [%s] : %s ", url.text(), url.attr("abs:href")));
}
}
}
你能帮我解决这个问题吗?
最佳答案
这实际上与您的代码无关。
特定网站使用 JavaScript 生成其部分内容。由于 Jsoup 只能获取网站的静态部分,因此您将无法如此轻松地对其进行抓取。
您仍然可以使用 Selenium 等工具为此,因为它们实际上在浏览器内执行 JavaScript 代码。
关于java - 使用 jsoup 通过 Java 加载嵌套链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40536592/