Java 爬虫代码示例:使用 Jsoup 抓取网页内容
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
// 使用Jsoup爬取网页内容
public class Crawler {
public static void main(String[] args) {
try {
// 定义网页地址
String url = 'http://example.com/';
// 使用Jsoup连接网页
Document docment = Jsoup.connect(url).get();
// 获取网页中的所有元素
Elements elements = docment.getAllElements();
// 遍历元素,获取需要的数据
for (Element element : elements) {
// 获取元素中的文本内容
String text = element.text();
// 打印文本内容
System.out.println(text);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址: https://www.cveoy.top/t/topic/lfSs 著作权归作者所有。请勿转载和采集!