int num = 1;
String path = "D:\\房源信息.txt";
BufferedWriter bf = new BufferedWriter(new FileWriter(path));
while(num<=100){
String link = "https://bj.lianjia.com/ershoufang/pg"+num;
Document document = Jsoup.connect(link).get();
Elements byClass = document.getElementsByClass("sellListContent");
Elements li = byClass.select("li");
for(int i = 0;i<li.size();i++){
String attr = li.get(i).select("a").attr("href");
String attrContent = "房源详情:"+attr;
bf.write(attrContent);
bf.newLine();
Element info = li.select("div.info.clear").get(i);
String title = info.select("div.title").text();
String titleContent = "房源标题:"+title;
bf.write(titleContent);
bf.newLine();
String addr = info.select("div.address").text();
String addrContent = "房源地址:"+addr;
bf.write(addrContent);
bf.newLine();
String louceng = info.select("div.flood").text();
String loucengContent = "房源楼层:"+louceng;
bf.write(loucengContent);
bf.newLine();
String parse = info.select("div.followInfo").text();
String [] content ;
String pail = " ";
content = parse.split(pail);
for (String por : content) {
String qiContent = "其他信息:"+por;
bf.write(qiContent);
bf.newLine();
}
bf.newLine();
}
num +=1;
}
bf.close();
来源:CSDN
作者:荒岛少年
链接:https://blog.csdn.net/weixin_42575310/article/details/90440638