1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【scrapy实践】_爬取安居客_广州_新楼盘数据

【scrapy实践】_爬取安居客_广州_新楼盘数据

时间:2021-02-03 22:25:33

相关推荐

【scrapy实践】_爬取安居客_广州_新楼盘数据

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

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

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

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

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

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

1、count.py

1 __author__ = 'Oscar_Yang' 2 #-*- coding= utf-8 -*- 3 """ 4查看mongodb存储状况的脚本count.py 5 """ 6 import time 7 import pymongo 8 client = pymongo.MongoClient("localhost", 27017) 9 db = client["SCRAPY_anjuke_gz"]10 sheet = db["anjuke_doc1"]11 12 while True:13print(sheet.find().count())14print("____________________________________")15time.sleep(3)

1 """2entrypoint.py3 """4 from scrapy.cmdline import execute5 execute(['scrapy', 'crawl', 'anjuke_gz'])

1 # -*- coding: utf-8 -*- 2 """ 3settings.py 4 """ 5 6 # Scrapy settings for anjuke_gz project 7 # 8 # For simplicity, this file contains only settings considered important or 9 # commonly used. You can find more settings consulting the documentation:10 #11 #/en/latest/topics/settings.html12 #/en/latest/topics/downloader-middleware.html13 #/en/latest/topics/spider-middleware.html14 15 BOT_NAME = 'anjuke_gz'16 17 SPIDER_MODULES = ['anjuke_gz.spiders']18 NEWSPIDER_MODULE = 'anjuke_gz.spiders'19 MONGODB_HOST = "127.0.0.1"20 MONGODB_PORT = 2701721 MONGODB_DBNAME="SCRAPY_anjuke_gz"22 MONGODB_DOCNAME="anjuke_doc1"23 24 # Crawl responsibly by identifying yourself (and your website) on the user-agent25 #USER_AGENT = 'anjuke_gz (+)'26 27 # Obey robots.txt rules28 ROBOTSTXT_OBEY = False29 30 # Configure maximum concurrent requests performed by Scrapy (default: 16)31 #CONCURRENT_REQUESTS = 3232 33 # Configure a delay for requests for the same website (default: 0)34 # See /en/latest/topics/settings.html#download-delay35 # See also autothrottle settings and docs36 #DOWNLOAD_DELAY = 337 # The download delay setting will honor only one of:38 #CONCURRENT_REQUESTS_PER_DOMAIN = 1639 #CONCURRENT_REQUESTS_PER_IP = 1640 41 # Disable cookies (enabled by default)42 #COOKIES_ENABLED = False43 44 # Disable Telnet Console (enabled by default)45 #TELNETCONSOLE_ENABLED = False46 47 # Override the default request headers:48 #DEFAULT_REQUEST_HEADERS = {49 # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',50 # 'Accept-Language': 'en',51 #}52 53 # Enable or disable spider middlewares54 # See /en/latest/topics/spider-middleware.html55 #SPIDER_MIDDLEWARES = {56 # 'anjuke_gz.middlewares.AnjukeGzSpiderMiddleware': 543,57 #}58 59 # Enable or disable downloader middlewares60 # See /en/latest/topics/downloader-middleware.html61 #DOWNLOADER_MIDDLEWARES = {62 # 'anjuke_gz.middlewares.MyCustomDownloaderMiddleware': 543,63 #}64 65 # Enable or disable extensions66 # See /en/latest/topics/extensions.html67 #EXTENSIONS = {68 # 'scrapy.extensions.telnet.TelnetConsole': None,69 #}70 71 # Configure item pipelines72 # See /en/latest/topics/item-pipeline.html73 ITEM_PIPELINES = {74 'anjuke_gz.pipelines.AnjukeGzPipeline': 300,75 }76 77 # Enable and configure the AutoThrottle extension (disabled by default)78 # See /en/latest/topics/autothrottle.html79 #AUTOTHROTTLE_ENABLED = True80 # The initial download delay81 #AUTOTHROTTLE_START_DELAY = 582 # The maximum download delay to be set in case of high latencies83 #AUTOTHROTTLE_MAX_DELAY = 6084 # The average number of requests Scrapy should be sending in parallel to85 # each remote server86 #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.087 # Enable showing throttling stats for every response received:88 #AUTOTHROTTLE_DEBUG = False89 90 # Enable and configure HTTP caching (disabled by default)91 # See /en/latest/topics/downloader-middleware.html#httpcache-middleware-settings92 HTTPCACHE_ENABLED = True93 HTTPCACHE_EXPIRATION_SECS = 094 HTTPCACHE_DIR = 'httpcache'95 HTTPCACHE_IGNORE_HTTP_CODES = []96 HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

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

1 # -*- coding: utf-8 -*- 2 3 # Define here the models for your scraped items 4 # 5 # See documentation in: 6 # /en/latest/topics/items.html 7 8 import scrapy 9 10 11 class AnjukeGzItem(scrapy.Item):12# define the fields for your item here like:13# name = scrapy.Field()14pass

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

# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: /en/latest/topics/item-pipeline.htmlimport pymongofrom scrapy.conf import settingsclass 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

1 from scrapy.http import Request 2 import scrapy 3 from bs4 import BeautifulSoup 4 import re 5 import requests 6 """ 7spider脚本 8 """ 9 class Myspider(scrapy.Spider):10name = 'anjuke_gz'11allowed_domains = ['http://gz./loupan/']12start_urls = ["http://gz./loupan/all/p{}/".format(i) for i in range(39)]13 14def parse(self, response):15 soup = BeautifulSoup(response.text,"lxml")16 content=soup.find_all(class_="items-name") #返回每个楼盘的对应数据17 for item in content:18 code=item["href"].split("/")[-1][:6]19 real_href="http://gz./loupan/canshu-{}.html?from=loupan_tab".format(code) #拼凑出楼盘详情页的url20 res=requests.get(real_href)21 soup = BeautifulSoup(res.text,"lxml")22 a = re.findall(r'<div class="name">(.*?)</div>', str(soup))23 b = soup.find_all(class_="des")24 data = {}25 for (i, j) in zip(range(len(b)), a):26 data[j] = b[i].text.strip().strip("\t")27 data["url"] = real_href28 yield data

下面是存入mongodb的情况。

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。