需求: 给定号码,获取其在百度网站中的号码标识,并输出到指定文件中。
需要使用的jar包:jsoup.jar
- 链接:https://pan.baidu.com/s/1ukyH7SiP7PN_YQBbmYQeeQ 提取码:dr3v
部分源码:
public String crawler(String str) {//str为txt文件中号码信息
try {
String url = "https://www.baidu.com/s?wd=" + URLEncoder.encode(str, "gb2312") + "&rn=100";
Document doc = Jsoup.connect(url).timeout(5000).get();
String result = doc.select("div[class=c-border op_fraudphone_container]")
.select("div[class=op_fraudphone_word]").select("strong").text();
if(result.isEmpty()){
System.out.println(str +"\t"+"null");
exportFile(str +"\t"+"null");
return null;
}else{
System.out.println(str +"\t"+result);
exportFile(str +"\t"+result);
return result;
}
} catch (Exception e) {
System.out.println("连接网站超时");
return null;
}
}
//写文件
public void exportFile(String str) {
File ob = new File(c:\\result.txt);
try {
FileWriter out = new FileWriter(ob, true);
out.write(str + "\n");
out.flush();
out.close();
} catch (IOException e) {
System.out.println(“写入文件出错”);
}
}