使用java实现一个简单的抓取安居客租房信息的爬虫

 最近,实验室的师兄马上就要毕业,要在工作的附近租个房子。于是自己就抱着玩一下的心态去安居客上爬取租房信息。

首先我是用的是Jsoup来分析html网页,从中获得我们所需要的数据。在得到这些数据之后,再将这些数据导入到我们本地的excel表格中,这里我使用了网上的一个开源工具包xxl-excel很好用。

接下来我们就可以开始动手爬取网页信息了。首先打开安居客的网页。先在网站上初步刷选。



我使用的是谷歌浏览器,按下F12选择Network查看网页给我们返回的请求。


在网页代码中有一个这样的标签,可以通过Jsoup解析出其中的内容。往下翻就可以查看到房子的一些信息。里面有房子所在的小区,房子的地址,房子结构,价格,简单介绍等信息。

 
 
 
 
xxl-excel配置信息
package anjuke;

import com.xuxueli.poi.excel.annotation.ExcelField;
import com.xuxueli.poi.excel.annotation.ExcelSheet;
import org.apache.poi.hssf.util.HSSFColor;

@ExcelSheet(name = "安居客房子", headColor = HSSFColor.HSSFColorPredefined.LIGHT_GREEN)
public class Anjuke {
    @ExcelField(name = "小区名字")
    private String name;
    @ExcelField(name = "价格")
    private int price;
    @ExcelField(name = "房子详情链接")
    private String url;
    @ExcelField(name = "房子地址")
    private String address;
    @ExcelField(name = "房子结构")
    private String house;
    @ExcelField(name = "房子面积")
    private String housesize;
    @ExcelField(name = "房子层数")
    private String housefloor;
    @ExcelField(name = "房子简介")
    private String title;

    public Wangyiyun(String name, int price, String url, String house, String housesize, String housefloor, String title, String address) {
        this.name = name;
        this.price = price;
        this.url = url;
        this.house = house;
        this.housesize = housesize;
        this.housefloor = housefloor;
        this.title = title;
        this.address = address;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public String getHouse() {
        return house;
    }

    public void setHouse(String house) {
        this.house = house;
    }

    public String getHousesize() {
        return housesize;
    }

    public void setHousesize(String housesize) {
        this.housesize = housesize;
    }

    public String getHousefloor() {
        return housefloor;
    }

    public void setHousefloor(String housefloor) {
        this.housefloor = housefloor;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

主程序:
 
 
package anjuke;

import com.xuxueli.poi.excel.ExcelExportUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        List<Anjuke> Anjukes = new ArrayList<>();
        for (int i = 0; ; i++) {
            Document document = Jsoup.connect("https://gz.zu.anjuke.com/fangyuan/liwan/" + "p" + String.valueOf(i) + "-x1-zj5345/").get();
            try {
                //判断网页中的页数是否超过了最后一页 如果超过了那么判断语句中的字符串会为空,如果没有超过最后一页会等于当前页码
                Elements elemen = document.getElementsByClass("multi-page");
                if (elemen.get(0).getElementsByClass("curr").text().equals("")) {
                    break;
                }
                Elements elements = document.getElementsByClass("zu-itemmod");
                for (Element element : elements) {
                    //title房子简单的一些介绍
                    String title = element.getElementsByTag("a").attr("title");
                    //房子详情链接
                    String url = element.getElementsByTag("a").attr("href");
                    //获得含有房屋详细信息的字符串
                    String result = element.getElementsByTag("address").text();
                    // System.out.print(result+"   ");
                    int b = result.indexOf(" ");
                    //System.out.println(a);
                    //int c=result.indexOf("-");
                    Elements elements2 = element.getElementsByTag("p");
                    String[] strings = elements2.first().text().split("\\|");
                    //房屋小区
                    String xiaoqu = result.substring(0, b);
                    //房屋所在的地址
                    String address = result.substring(b, result.length());
                    //获得月租金价格,int类型数据
                    int price = Integer.valueOf(element.getElementsByClass("zu-side").first().getElementsByTag("strong").text());
                    //房屋类型
                    String houseType = strings[0];
                    //房子大小
                    String housesize = strings[1];
                    //房子的楼层
                    String decoration = strings[2];
                    String floor = decoration.substring(0, decoration.length() - 4);
                    Anjuke housedetail = new Wangyiyun(xiaoqu, price, url, houseType, housesize, floor, title, address);
                    Anjukes.add(housedetail);
                }
                //dbHelper.close();
            } catch (ArrayIndexOutOfBoundsException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            //将集合中存入的房子信息写入到本地的excel文件中
            ExcelExportUtil.exportToFile(Anjukes, "C:\\crawler\\anjuke.xls");
        }
    }

}
一共抓了600多条数据,结果如下。使用Jsoup的关键还是去看我们的html文件,再一个个进行解析,得到我们想要的结果。
如果你觉得对你有用的话,记得点赞哦。 
 
转载记得需要标明原文链接


 



猜你喜欢

转载自blog.csdn.net/gottawangzi/article/details/80412937
今日推荐