需求:爬取【安居客—广州—新楼盘】的数据,具体到每个楼盘的详情页的若干字段。

难点:楼盘类型各式各样:住宅 别墅 商住 商铺 写字楼,不同楼盘字段的名称不一样。然后同一种类型,比如住宅,又分为不同的情况,比如分为期房在售,现房在售,待售,尾盘。其他类型也有类似情况。所以字段不能设置固定住。

解决方案:目前想到的解决方案,第一种:scrapy中items.py中不设置字段,spider中爬的时候自动识别字段(也就是有啥字段就保留下来),然后返回字典存起来。第二种,不同字段的网页分别写规则单独抓取。显然不可取。我采用的是第一种方案。还有其他方案的朋友们,欢迎交流哈。

目标网址为:http://gz.fang.anjuke.com/ 该网页下的楼盘数据

示例楼盘网址:http://gz.fang.anjuke.com/loupan/canshu-298205.html?from=loupan_tab

开始编写scrapy脚本。建立工程步骤略过。

1、count.py

 __author__ = 'Oscar_Yang'
#-*- coding= utf-8 -*-
"""
查看mongodb存储状况的脚本count.py
"""
import time
import pymongo
client = pymongo.MongoClient("localhost", 27017)
db = client["SCRAPY_anjuke_gz"]
sheet = db["anjuke_doc1"] while True:
print(sheet.find().count())
print("____________________________________")
time.sleep(3)
 """
entrypoint.py
"""
from scrapy.cmdline import execute
execute(['scrapy', 'crawl', 'anjuke_gz'])
 # -*- coding: utf-8 -*-
"""
settings.py
""" # Scrapy settings for anjuke_gz project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# http://doc.scrapy.org/en/latest/topics/settings.html
# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html BOT_NAME = 'anjuke_gz' SPIDER_MODULES = ['anjuke_gz.spiders']
NEWSPIDER_MODULE = 'anjuke_gz.spiders'
MONGODB_HOST = "127.0.0.1"
MONGODB_PORT = 27017
MONGODB_DBNAME="SCRAPY_anjuke_gz"
MONGODB_DOCNAME="anjuke_doc1" # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'anjuke_gz (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = False # Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32 # Configure a delay for requests for the same website (default: 0)
# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16 # Disable cookies (enabled by default)
#COOKIES_ENABLED = False # Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False # Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#} # Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'anjuke_gz.middlewares.AnjukeGzSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'anjuke_gz.middlewares.MyCustomDownloaderMiddleware': 543,
#} # Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#} # Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'anjuke_gz.pipelines.AnjukeGzPipeline': 300,
} # Enable and configure the AutoThrottle extension (disabled by default)
# See http://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False # Enable and configure HTTP caching (disabled by default)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
HTTPCACHE_ENABLED = True
HTTPCACHE_EXPIRATION_SECS = 0
HTTPCACHE_DIR = 'httpcache'
HTTPCACHE_IGNORE_HTTP_CODES = []
HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

接下来,是items。因为没有设置字段,为默认的代码。

 # -*- coding: utf-8 -*-

 # Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy class AnjukeGzItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
pass

接下来,是piplines.py。在中设置了mongodb的配置。

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html import pymongo
from scrapy.conf import settings class AnjukeGzPipeline(object):
def __init__(self):
host=settings["MONGODB_HOST"]
port=settings["MONGODB_PORT"]
dbname=settings["MONGODB_DBNAME"]
client=pymongo.MongoClient(port=port,host=host)
tdb = client[dbname]
self.post=tdb[settings["MONGODB_DOCNAME"]]
def process_item(self,item,spider):
info = dict(item)
self.post.insert(info)
return item

最后,是最主要的spider.py

 from scrapy.http import Request
import scrapy
from bs4 import BeautifulSoup
import re
import requests
"""
spider脚本
"""
class Myspider(scrapy.Spider):
name = 'anjuke_gz'
allowed_domains = ['http://gz.fang.anjuke.com/loupan/']
start_urls = ["http://gz.fang.anjuke.com/loupan/all/p{}/".format(i) for i in range(39)] def parse(self, response):
soup = BeautifulSoup(response.text,"lxml")
content=soup.find_all(class_="items-name") #返回每个楼盘的对应数据
for item in content:
code=item["href"].split("/")[-1][:6]
real_href="http://gz.fang.anjuke.com/loupan/canshu-{}.html?from=loupan_tab".format(code) #拼凑出楼盘详情页的url
res=requests.get(real_href)
soup = BeautifulSoup(res.text,"lxml")
a = re.findall(r'<div class="name">(.*?)</div>', str(soup))
b = soup.find_all(class_="des")
data = {}
for (i, j) in zip(range(len(b)), a):
data[j] = b[i].text.strip().strip("\t")
data["url"] = real_href
yield data

下面是存入mongodb的情况。

  因为针对不同的网页结构,爬取的规则是一个,所以爬取的时候就不能针对每个字段进行爬取,所以存到库里的数据如果要是分析的话还需要清洗。

在python中使用mongodb的查询语句,再配合使用pandas应该就很方便清洗了。

【scrapy实践】_爬取安居客_广州_新楼盘数据的更多相关文章

  1. Python开发爬虫之BeautifulSoup解析网页篇:爬取安居客网站上北京二手房数据

    目标:爬取安居客网站上前10页北京二手房的数据,包括二手房源的名称.价格.几室几厅.大小.建造年份.联系人.地址.标签等. 网址为:https://beijing.anjuke.com/sale/ B ...

  2. PyCharm+Scrapy爬取安居客楼盘信息

    一.说明 1.1 开发环境说明 开发环境--PyCharm 爬虫框架--Scrapy 开发语言--Python 3.6 安装第三方库--Scrapy.pymysql.matplotlib 数据库--M ...

  3. python3 [爬虫实战] selenium 爬取安居客

    我们爬取的网站:https://www.anjuke.com/sy-city.html 获取的内容:包括地区名,地区链接: 安居客详情 一开始直接用requests库进行网站的爬取,会访问不到数据的, ...

  4. python爬取安居客二手房网站数据(转)

    之前没课的时候写过安居客的爬虫,但那也是小打小闹,那这次呢, 还是小打小闹 哈哈,现在开始正式进行爬虫书写 首先,需要分析一下要爬取的网站的结构: 作为一名河南的学生,那就看看郑州的二手房信息吧! 在 ...

  5. Python-新手爬取安居客新房房源

    新手,整个程序还有很多瑕疵. 1.房源访问的网址为城市的拼音+后面统一的地址.需要用到xpinyin库 2.用了2种解析网页数据的库bs4和xpath(先学习的bs4,学了xpath后部分代码改成xp ...

  6. python爬虫爬取安居客并进行简单数据分析

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 爬取过程一.指定爬取数据二.设置请求头防止反爬三.分析页面并且与网页源码进行比对四.分析页面整理数据 ...

  7. python3 爬虫之爬取安居客二手房资讯(第一版)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa import requests from bs4 import Beau ...

  8. 安居客scrapy房产信息爬取到数据可视化(下)-可视化代码

    接上篇:安居客scrapy房产信息爬取到数据可视化(下)-可视化代码,可视化的实现~ 先看看保存的数据吧~ 本人之前都是习惯把爬到的数据保存到本地json文件, 这次保存到数据库后发现使用mongod ...

  9. scrapy爬取极客学院全部课程

    # -*- coding: utf-8 -*- # scrapy爬取极客学院全部课程 import scrapy from pyquery import PyQuery as pq from jike ...

随机推荐

  1. 01 Spring框架 基本介绍

    相信学习java,并且走Web道路的道友都应该知道Spring的大名,它的地位相信也不需要我在这里多说什么,接下来的文章就Spring的配置和使用来进行一些讲解. 首先学习框架我们都要考虑和做到以下几 ...

  2. css系列(6)css的运用(二)

        本节继续介绍css在html页面重的应用实例.     (1)div的border-style属性: none: 无样式 hidden: 除了同表格的边框发生冲突的时候,其它同none dot ...

  3. $《第一行代码:Android》读书笔记——第10章 Android网络编程

    (一)WebView的用法 1.WebView也是一个普通的控件. 2.常用用法: WebView webView = (WebView)findViewById(R.id.web_view); we ...

  4. 024_MapReduce中的基类Mapper和基类Reducer

    内容提纲 1) MapReduce中的基类Mapper类,自定义Mapper类的父类. 2) MapReduce中的基类Reducer类,自定义Reducer类的父类. 1.Mapper类 API文档 ...

  5. 在freescale mx6q平台上添加spi资源

    1:配置管脚为SPI功能 在board-mx6q_sabresd.h的最后添加,复制被重定义 (以添加SPI2为例) <span style="font-size:18px;" ...

  6. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...

  7. java 跨数据库导入大数据

    java 跨数据库导入大数据 /** * java程序跨服务器跨数据库批量导入导出百万级数据 * @param args * @throws Exception */ public static vo ...

  8. VMware 连接不上XSHELL

    本人创建虚拟机时把网络连接模式选成了桥接,后来在VMware虚拟网络编辑器中查看了连接模式:NAT. 重新回到VMware中更改了连接模式:NAT模式 成功连接XSHELL 步骤1 打开VMware的 ...

  9. Go 内置库 IO interface

    基本的 IO 接口 io 包为 I/O 原语提供了基本的接口.它主要包装了这些原语的已有实现. 由于这些接口和原语以不同的实现包装了低级操作,因此除非另行通知,否则客户端不应假定它们对于并行执行是安全 ...

  10. scapy学习笔记(3)

    转自:@小五义:http://www.cnblogs/xiaowuyi 在安装完scapy(前两篇笔记有介绍)后,linux环境下,执行sudo scapy运行scapy. 一.简单的发送包 1.se ...