jsoup https://jsoup.org/
1) 下载 jsoup-1.12.1.jar
2) idea 建立项目,项目设置引入 jar模块
3) 写代码实现爬虫
package org.kenniu;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.swing.*;
import java.io.IOException;
public class users {
public static void main(String[] args) throws IOException {
String w = JOptionPane.showInputDialog("请输入单词:");
String u = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+w+"&rsv_pq=80b39cac008d648b&rsv_t=55b1EaS9HzawgKsAEHUYgY2Tb0SJJIK8OYGEkjTbAyqIrukjIyQ7HzghY6o&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=5&rsv_sug1=3&rsv_sug7=101&rsv_sug2=0&inputT=767&rsv_sug4=1418";
System.out.println(u);
Document doc = Jsoup.connect(u).get();
Elements es = doc.select("span[class=op_dict_text2]");
System.out.println(doc.body().text());
StringBuilder s = new StringBuilder();;
es.forEach(e -> s.append(e.html()));
JOptionPane.showMessageDialog(null,s.toString(),w,1);
}
}
2、maven
使用maven建立项目
根目录下有一个pom.xml 文件
添加
使用国内镜像