java爬取链家网数据

    xiaoxiao2022-07-02  115

    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();

     

    最新回复(0)