全面采集二手房数据:

网站二手房总数据量为27650条,但有的参数字段会出现一些问题,因为只给返回100页数据,具体查看就需要去细分请求url参数去请求网站数据。
我这里大概的获取了一下筛选条件参数,一些存在问题也没做细化处理,大致的采集数据量为21096,实际19794条。 看一下执行完成结果:

{'downloader/exception_count': 199,
'downloader/exception_type_count/twisted.internet.error.NoRouteError': 192,
'downloader/exception_type_count/twisted.web._newclient.ResponseNeverReceived': 7,
'downloader/request_bytes': 9878800,
'downloader/request_count': 21096,
'downloader/request_method_count/GET': 21096,
'downloader/response_bytes': 677177525,
'downloader/response_count': 20897,
'downloader/response_status_count/200': 20832,
'downloader/response_status_count/301': 49,
'downloader/response_status_count/302': 11,
'downloader/response_status_count/404': 5,
'dupefilter/filtered': 53,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 12, 8, 49, 42, 371235),
'httperror/response_ignored_count': 5,
'httperror/response_ignored_status_count/404': 5,
'log_count/DEBUG': 21098,
'log_count/ERROR': 298,
'log_count/INFO': 61,
'request_depth_max': 3,
'response_received_count': 20837,
'retry/count': 199,
'retry/reason_count/twisted.internet.error.NoRouteError': 192,
'retry/reason_count/twisted.web._newclient.ResponseNeverReceived': 7,
'scheduler/dequeued': 21096,
'scheduler/dequeued/memory': 21096,
'scheduler/enqueued': 21096,
'scheduler/enqueued/memory': 21096,
'spider_exceptions/TypeError': 298,
'start_time': datetime.datetime(2018, 11, 12, 7, 59, 52, 608383)}
2018-11-12 16:49:42 [scrapy.core.engine] INFO: Spider closed (finished)

采集数据如图:

num = 296910/15=19794条

2. lianjia.py

# -*- coding: utf-8 -*-
import scrapy class LianjiaSpider(scrapy.Spider):
name = 'lianjia'
allowed_domains = ['gz.lianjia.com']
start_urls = ['https://gz.lianjia.com/ershoufang/pg1/']
  
def parse(self, response):
for i in range(1,8):
for j in range(1,8):
url = 'https://gz.lianjia.com/ershoufang/p{}a{}pg1'.format(i,j)
yield scrapy.Request(url=url,callback=self.parse_detail) def parse_detail(self,response):
# 符合筛选条件的个数
counts = response.xpath("//h2[@class='total fl']/span/text()").extract_first().strip()
# print(counts) if int(counts)%30 >0:
p_num = int(counts)//30+1
# print(p_num)
# 拼接首页url
for k in range(1,p_num+1):
url = response.url
link_url = url.split('pg')[0]+'pg{}/'.format(k)
# print(link_url)
yield scrapy.Request(url=link_url,callback=self.parse_detail2) def parse_detail2(self,response):
#获取当前页面url
link_urls = response.xpath("//div[@class='info clear']/div[@class='title']/a/@href").extract()
for link_url in link_urls:
# print(link_url)
yield scrapy.Request(url=link_url,callback=self.parse_detail3)
# print('*'*100) def parse_detail3(self,response):
title = response.xpath("//div[@class='title']/h1[@class='main']/text()").extract_first()
print('标题: '+ title)
dist = response.xpath("//div[@class='areaName']/span[@class='info']/a/text()").extract_first()
print('所在区域: '+ dist)
contents = response.xpath("//div[@class='introContent']/div[@class='base']")
# print(contents)
house_type = contents.xpath("./div[@class='content']/ul/li[1]/text()").extract_first()
print('房屋户型: '+ house_type)
floor = contents.xpath("./div[@class='content']/ul/li[2]/text()").extract_first()
print('所在楼层: '+ floor)
built_area = contents.xpath("./div[@class='content']/ul/li[3]/text()").extract_first()
print('建筑面积: '+ built_area)
family_structure = contents.xpath("./div[@class='content']/ul/li[4]/text()").extract_first()
print('户型结构: '+ family_structure)
inner_area = contents.xpath("./div[@class='content']/ul/li[5]/text()").extract_first()
print('套内面积: '+ inner_area)
architectural_type = contents.xpath("./div[@class='content']/ul/li[6]/text()").extract_first()
print('建筑类型: '+ architectural_type)
house_orientation = contents.xpath("./div[@class='content']/ul/li[7]/text()").extract_first()
print('房屋朝向: '+ house_orientation)
building_structure = contents.xpath("./div[@class='content']/ul/li[8]/text()").extract_first()
print('建筑结构: '+ building_structure)
decoration_condition = contents.xpath("./div[@class='content']/ul/li[9]/text()").extract_first()
print('装修状况: '+ decoration_condition)
proportion = contents.xpath("./div[@class='content']/ul/li[10]/text()").extract_first()
print('梯户比例: '+ proportion)
elevator = contents.xpath("./div[@class='content']/ul/li[11]/text()").extract_first()
print('配备电梯: '+ elevator)
age_limit =contents.xpath("./div[@class='content']/ul/li[12]/text()").extract_first()
print('产权年限: '+ age_limit)
# try:
# house_label = response.xpath("//div[@class='content']/a/text()").extract_first()
# except:
# house_label = ''
# print('房源标签: ' + house_label)
with open('text2', 'a', encoding='utf-8')as f:
f.write('\n'.join(
[title,dist,house_type,floor,built_area,family_structure,inner_area,architectural_type,house_orientation,building_structure,decoration_condition,proportion,elevator,age_limit]))
f.write('\n' + '=' * 50 + '\n')
print('-'*100)
3.代码还需要细分的话,就多配置url的请求参数,缩小筛选范围,获取页面就更精准,就能避免筛选到过3000的数据类型,可以再去细分。

44.scrapy爬取链家网站二手房信息-2的更多相关文章

  1. 43.scrapy爬取链家网站二手房信息-1

    首先分析:目的:采集链家网站二手房数据1.先分析一下二手房主界面信息,显示情况如下: url = https://gz.lianjia.com/ershoufang/pg1/显示总数据量为27589套 ...

  2. Python——Scrapy爬取链家网站所有房源信息

    用scrapy爬取链家全国以上房源分类的信息: 路径: items.py # -*- coding: utf-8 -*- # Define here the models for your scrap ...

  3. python - 爬虫入门练习 爬取链家网二手房信息

    import requests from bs4 import BeautifulSoup import sqlite3 conn = sqlite3.connect("test.db&qu ...

  4. python爬虫:利用BeautifulSoup爬取链家深圳二手房首页的详细信息

    1.问题描述: 爬取链家深圳二手房的详细信息,并将爬取的数据存储到Excel表 2.思路分析: 发送请求--获取数据--解析数据--存储数据 1.目标网址:https://sz.lianjia.com ...

  5. Python的scrapy之爬取链家网房价信息并保存到本地

    因为有在北京租房的打算,于是上网浏览了一下链家网站的房价,想将他们爬取下来,并保存到本地. 先看链家网的源码..房价信息 都保存在 ul 下的li 里面 ​ 爬虫结构: ​ 其中封装了一个数据库处理模 ...

  6. Python爬取链家二手房源信息

    爬取链家网站二手房房源信息,第一次做,仅供参考,要用scrapy.   import scrapy,pypinyin,requests import bs4 from ..items import L ...

  7. python3 爬虫教学之爬取链家二手房(最下面源码) //以更新源码

    前言 作为一只小白,刚进入Python爬虫领域,今天尝试一下爬取链家的二手房,之前已经爬取了房天下的了,看看链家有什么不同,马上开始. 一.分析观察爬取网站结构 这里以广州链家二手房为例:http:/ ...

  8. Scrapy实战篇(一)之爬取链家网成交房源数据(上)

    今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...

  9. python爬虫:爬取链家深圳全部二手房的详细信息

    1.问题描述: 爬取链家深圳全部二手房的详细信息,并将爬取的数据存储到CSV文件中 2.思路分析: (1)目标网址:https://sz.lianjia.com/ershoufang/ (2)代码结构 ...

随机推荐

  1. jQuery 事件绑定四种方式,delegate委托强大绑定在3.0中修改为on

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 在Win32程序中显示Dos调试窗口

    在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料. 用到的API函数如下: / ...

  3. apache配置https协议

    安装openssl有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.下面对两种方式均进行详细描述. 一.下载和安装openss 方法一:直接使用openssl安装包 W ...

  4. linux的系统组成和计算机组成原理,linux常用操作

    Linux入门 linux简介   学习目的:linux服务器操作系统稳定长期运行,python,pycharm装于linux上 linux系统组成 应用软件:调用系统软件接口 linux操作系统分两 ...

  5. 浅谈负margin

    通常来说margin为正值时,我们很清楚其布局形式,即在border边界线处再往外扩展指定长度.可margin为负又表示什么呢,表示始于border边界线处并向内扩展指定长度,这样,下一个文档流对象便 ...

  6. 黄聪:FFmpeg视频转码技巧之-crf参数(H.264篇)

    昨天,有个朋友给我出了个难题:他手上有一个视频,1080P的,49秒,200多兆:要求在确保质量的情况下把文件压缩到10M以内. 这是什么概念呢?按照文件大小10M来计算,码率是:10 x 8 / 4 ...

  7. 解决读取Excel表格中某列数据为空的问题 c#

    解决同一列中“字符串”和“数字”两种格式同时存在,读取时,不能正确显示“字符串”格式的问题:set xlsconn=CreateObject("ADODB.Connection") ...

  8. java.util.ConcurrentModificationException的解决办法

    今天在使用iterator.hasNext()操作迭代器的时候,当迭代的对象发生改变,比如插入了新数据,或者有数据被删除. 编译器报出了以下异常: Exception in thread " ...

  9. Java连接S3并上传Redis

    package com.shinho.bigdatalake.redis; import com.amazonaws.regions.Region; import com.amazonaws.regi ...

  10. Android keystore相关

    一.生成keystorekeytool -genkey -alias test.keystore -keyalg RSA -validity -keystore test.keystore 二.查看 ...