java爬取链家网数据

两盒软妹~` 提交于 2019-12-08 21:08:13
	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();
		
		

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!