分析:
、、爬虫的结构分为三部分:
1、获取网页链接并解析网页,
2、将解析好的信息,临时存储到写好的类对象里(有可能跳过这一步)
3、将缓存信息传入云端数据库
**使用的工具:**eclipse
、、本次主要讲网页的链接和解析:
、、由于二手房网页是静态网页,所以使用了Java的第三方包jsoup处理
PS:jsoup是Java一款强大的HTML解析工具,用于解析网页相当方便,在此就不详述了。
、、首先分析房屋网页:
//***********************************重庆二手房****************************//
//写出方法,获取二手房每一页的所有房源地址
public static String[] getEershoufangUrlList(int page) throws Exception {
String pageUrl =
//传入每一页,拼接成地址
"https://cq.ke.com/ershoufang/pg" +page+ "/";
Connection con = Jsoup.connect(pageUrl);
con.header("User-Agent",user_agent);
Document doc = con.get();
Elements lis = doc.getElementsByClass("sellListContent").get(0).getElementsByClass("title");
int sum = lis.size();
String[] urlList = new String[sum];
//获取每一条地址
for (int i = 0; i < sum; i++) {
urlList[i] = lis.get(i).getElementsByTag("a").get(0).attr("href");
}
return urlList;
}
// 简单解释一下:pageUrl就是每一页列表页面地址,通过封装的方法获取整个网页,再获取其element元素里面对应的房屋页面地址,返回房屋地址数组。
// PS:值得注意的是,一般这种网站存在反爬虫机制,所以设置一个user-agent来伪装成浏览器获取网页,不过这只能处理简单的反爬虫,如果遇到复杂的反爬虫,就很难奏效了
//浏览器代理,防止反爬虫
private static String user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2727.400";
2、然后通过获取到的每一个房屋地址,用jsoup获取到房屋页面:
//页面信息处理
public static SecondHouse getErshoufangInfo(String urlStr) throws Exception {
//创建房屋对象
SecondHouse house = new SecondHouse();
Connection con = Jsoup.connect(urlStr);
con.header("User-Agent",user_agent);
Document doc = con.get();
PS: SecondHouse是我自建的用于存储解析出来的房屋信息的类
这是要解析出所需信息的页面:
、、 哦,由于本人基本不会正则表达式,所以全程仅使用了一个表达式:
toString().replaceAll("</?[^>]+>", "").trim()
、、这一段就是将包含对应信息的HTML语句转换成去掉HTML标签且首位没有空格的字符串,事实上就这一段语句完全足够用于解析了。
还有就是写一个方法用于提取字符串里面的数字,这个相当重要:
//***************************类辅助方法***********************************//
//从字符串里面提取数字,只提取整数部分,作为辅助函数使用
private static int getNumFromStr(String str) {
str = str.trim();
String strTemp = "";
int num = 0;
for (int i = 0; i < str.length(); i++) {
//只取整数部分
if (str.charAt(i) == '.') {
break;
}
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
strTemp += str.charAt(i);
}
}
if (strTemp.length() == 0) {
return 0;
}
num = Integer.parseInt(strTemp);
return num;
}
以下是jsoup解析房屋网页的具体方法:
//页面信息处理
public static SecondHouse getErshoufangInfo(String urlStr) throws Exception {
//创建房屋对象
SecondHouse house = new SecondHouse();
Connection con = Jsoup.connect(urlStr);
con.header("User-Agent",user_agent);
Document doc = con.get();
// Test.print(doc.toString());
//获取标题
String title = doc.getElementsByClass("title-wrapper").get(0).getElementsByClass("main").get(0).attr("title");
// Test.print(title);
house.setElemName(title);
Element houseInfo = doc.getElementsByClass("overview").get(0).getElementsByClass("content").get(0);
// Test.print(houseInfo.toString());
//获取每一项信息写入对象
// replaceAll("</?[^>]+>", "");
//写入总价
int price = getNumFromStr(houseInfo.getElementsByClass("total").toString());
house.setPrice(price);
//写入单价
int unit_price = getNumFromStr(houseInfo.getElementsByClass("unitPriceValue").toString());
// Test.print(unit_price+"");
house.setUnit_price(unit_price);
//写入面积
house.setArea(price*10000/unit_price);
//
Elements mainInfos = houseInfo.getElementsByClass("mainInfo");
//写入户型
String houseStyle = mainInfos.get(0).toString().replaceAll("</?[^>]+>", "").trim();
house.setHouseStyle(houseStyle);
String direction = mainInfos.get(1).toString().replaceAll("</?[^>]+>", "").trim();
house.setDirection(direction);
//
Elements subInfos = houseInfo.getElementsByClass("subInfo");
//写入楼层
String floor = subInfos.get(0).toString().replaceAll("</?[^>]+>", "").trim();
house.setFloor(floor);
String decoration = subInfos.get(1).toString().replaceAll("</?[^>]+>", "").trim();
house.setDecoration(decoration);
int buildTime = getNumFromStr(subInfos.get(2).toString());
house.setBuildTime(buildTime);
//获取小区名和所在区域
Elements infos = houseInfo.getElementsByClass("info");
String community = infos.get(0).toString().replaceAll("</?[^>]+>", "");
house.setCommunity(community);
String location = infos.get(1).toString().replaceAll("</?[^>]+>", "").substring(0, 2);
house.setLocation(location);
/\
//获取关注人数
int viewerSum = getNumFromStr(doc.getElementById("favCount").toString());
house.setViewerSum(viewerSum);
//Test.print(viewerSum+ "");
// SecondHouse.printHouseInfo(house);
return house;
}
、、嗯,关于jsoup解析页面元素的具体用法在这里就不再一一说明了,我就用了几个比较常用的getElement方法,兴趣的朋友可以自己搜一下。
另:关于解析下来的房屋信息暂时存储下次再写。还有就是这是我第一次写Java爬虫,还相当生疏,有可以优化和不足的地方希望大佬指点。