首先分析:
目的:采集链家网站二手房数据
1.先分析一下二手房主界面信息,显示情况如下: url = https://gz.lianjia.com/ershoufang/pg1/
显示总数据量为27589套,但是页面只给返回100页的数据,每页30条数据,也就是只给返回3000条数据。

2.再看一下筛选条件的情况:

100万以下(775):https://gz.lianjia.com/ershoufang/pg1p1/(p1是筛选条件参数,pg1是页面参数)  页面返回26页信息
100万-120万(471):https://gz.lianjia.com/ershoufang/pg1p2/ 页面返回16页信息 以此类推也就是网站只给你返回查看最多100页,3000条的数据,登陆的话情况也是一样的情况。
3.采集代码如下:
这个是 linjia.py 文件,这里需要注意的问题就是 setting里要设置
ROBOTSTXT_OBEY = False,不然页面不给返回数据。
# -*- 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): #获取当前页面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_detail)
print('*'*100) #翻页
for i in range(1,101):
url = 'https://gz.lianjia.com/ershoufang/pg{}/'.format(i)
# print(url)
yield scrapy.Request(url=url,callback=self.parse) def parse_detail(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)
# decoration_description = response.xpath("//div[@class='baseattribute clear'][1]/div[@class='content']/text()").extract_first()
# print('装修描述 '+ decoration_description)
# community_introduction = response.xpath("//div[@class='baseattribute clear'][2]/div[@class='content']/text()").extract_first()
# print('小区介绍: '+ community_introduction)
# huxing_introduce = response.xpath("//div[@class='baseattribute clear']3]/div[@class='content']/text()").extract_first()
# print('户型介绍: '+ huxing_introduce)
# selling_point = response.xpath("//div[@class='baseattribute clear'][4]/div[@class='content']/text()").extract_first()
# print('核心卖点: '+ selling_point)
# 以追加的方式及打开一个文件,文件指针放在文件结尾,追加读写!
with open('text', '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,house_label]))
f.write('\n' + '=' * 50 + '\n')
print('-'*100)
4.这里采集的是全部,没设置筛选条件,只返回100也数据。
采集数据情况如下:
这里只采集了15个字段信息,其他的数据没采集。
采集100页,算一下拿到了2704条数据。

4.这个是上周写的,也没做修改完善,之后会对筛选条件url进行整理,尽量采集网站多的数据信息,可以通过获取筛选条件分别获取完整数据。

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

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

    全面采集二手房数据: 网站二手房总数据量为27650条,但有的参数字段会出现一些问题,因为只给返回100页数据,具体查看就需要去细分请求url参数去请求网站数据.我这里大概的获取了一下筛选条件参数,一 ...

  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. SDI core端口说明

    SDI core端口说明 本文基于赛灵思的官方文档以及自己的理解: 1.生成SDI core 2.得到SDI core的顶层文件,并对每个端口做出解释 smpte_sdi smpte_sdi ( .r ...

  2. Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014

    传统感念:每次修改完nginx配置文件,要重启nginx 动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息. Nginx: 反向代理和负载均衡 Consul:是用go编写 ...

  3. Ubuntu16.04系统重装***

    首先准备一个Live CD,就是Ubuntu的安装盘. 备份原理就是将系统文件压缩打包.由于Linux系统所有都是文件,故,只需要将系统打包即可.当然,必须除了当前系统运行中的文件以及临时文件. 打包 ...

  4. sqlserver 全局事务查询

    -- 此语句用于查看最老的活动事务.未完成的分布式事务或复制事务的信息. dbcc opentran -- 通过动态管理视图查看活动事务 select*from sys.dm_tran_active_ ...

  5. mass种子模块看完了

    作者当然也不容易,要考虑各种兼容问题,要考虑效率问题(他真的考虑过吗,我表示强烈怀疑,貌似仅仅是风格上模仿其他源码) 相当无语. 本来我是知道的,代码 调试的过程中逐渐完善,逐渐与各种兼容问题和预想不 ...

  6. 黄聪:C# webBrowser控件禁用alert,confirm之类的弹窗解决方案

    同样的代码,我尝试了很多次都没有成功.最后终于成功了,是因为我没有在正确的事件里面调用这段代码. private void InjectAlertBlocker() { HtmlElement hea ...

  7. elasticsearch 口水篇(4)java客户端 - 原生esClient

    上一篇(elasticsearch 口水篇(3)java客户端 - Jest)Jest是第三方客户端,基于REST Api进行调用(httpClient),本篇简单介绍下elasticsearch原生 ...

  8. 【mysql】IP地址整数int和varchar的转换

    mysql中IP地址的存储 IP:如192.168.12.145,在存储时,若是采用varchar进行存储,存在两个主要缺点: 存储空间占用较大: 查询检索较慢: 解决方式: 存储时:将字符串类型的I ...

  9. 【剑指offer】两个栈实现队列

    用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. public class Solution {        Stack<Integer> stack ...

  10. python-selenium 并发执行用例的问题

    看了虫师的多进程执行测试用例一直都执行错误,最后解决了 解决方法如下: 使用threading模块 import threading 使用threading.Thread的方法 ,执行用例成功