加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

爬取两万多租房数据,告诉你广州房租现状

发布时间:2018-12-22 02:29:42 所属栏目:教程 来源:zone7
导读:概述 前言 统计结果 爬虫代码实现 爬虫分析实现 后记 前言 建议在看这篇文章之前,请看完这三篇文章,因为本文是依赖于前三篇文章的: 爬虫利器初体验(1) 听说你的爬虫又被封了?(2) 爬取数据不保存,就是耍流氓(3) 八月份的时候,由于脑洞大开,决定用 pyt

最后解析一个页面的数据

  1. def parse(self, response): # 解析一个页面的数据 
  2.     self.logger.info("==========================") 
  3.     soup = BeautifulSoup(response.body, "html5lib") 
  4.     divs = soup.find_all("dd", attrs={"class": "info rel"})  # 获取需要爬取得 div 
  5.     for div in divs: 
  6.         ps = div.find_all("p") 
  7.         try:  # 捕获异常,因为页面中有些数据没有被填写完整,或者被插入了一条广告,则会没有相应的标签,所以会报错 
  8.             for index, p in enumerate(ps):  # 从源码中可以看出,每一条 p 标签都有我们想要的信息,故在此遍历 p 标签, 
  9.                 text = p.text.strip() 
  10.                 print(text)  # 输出看看是否为我们想要的信息 
  11.             roomMsg = ps[1].text.split("|") 
  12.             area = roomMsg[2].strip()[:len(roomMsg[2]) - 1] 
  13.             item = RenthousescrapyItem() 
  14.             item["title"] = ps[0].text.strip() 
  15.             item["rooms"] = roomMsg[1].strip() 
  16.             item["area"] = int(float(area)) 
  17.             item["price"] = int(ps[len(ps) - 1].text.strip()[:len(ps[len(ps) - 1].text.strip()) - 3]) 
  18.             item["address"] = ps[2].text.strip() 
  19.             item["traffic"] = ps[3].text.strip() 
  20.             if (self.baseUrl+"house/") in response.url: # 对不限区域的地方进行区分 
  21.                 item["region"] = "不限" 
  22.             else: 
  23.                 item["region"] = ps[2].text.strip()[:2] 
  24.             item["direction"] = roomMsg[3].strip() 
  25.             print(item) 
  26.             yield item 
  27.         except: 
  28.             print("糟糕,出现 exception") 
  29.             continue 
  30.     if len(self.allUrlList) != 0:  
  31.         url = self.allUrlList.pop(0) 
  32.         yield Request(url, callback=self.parse, dont_filter=True) 

数据分析实现

这里主要通过 pymongo 的一些聚合运算来进行统计,再结合相关的图标库,来进行数据的展示。

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读