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

爬取两万多租房数据,汇报你广州房租近况

发布时间:2018-12-21 11:09:17 所属栏目:教程 来源: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 的一些聚合运算来举办统计,再团结相干的图标库,来举办数据的展示。

(编辑:湖南网)

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

热点阅读