美文网首页
爬虫代码--我爱我家(小区成交)

爬虫代码--我爱我家(小区成交)

作者: ISeeMoon | 来源:发表于2017-11-08 00:17 被阅读0次

先放代码,具体过程明天再说。

Scrapy结构

定义item部分

import scrapy

class WawjItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    blockurl = scrapy.Field()
    test = scrapy.Field()
    blockname = scrapy.Field()
    houseinfo = scrapy.Field()
    housearea = scrapy.Field()
    data = scrapy.Field()
    totalprice = scrapy.Field()
    unitprice = scrapy.Field()

Spider部分

# -*- coding: utf-8 -*-
import scrapy
from wawj.items import WawjItem
from scrapy.http import Request

class WawjspiderSpider(scrapy.Spider):
    name = 'w2spider'
    start_urls = ['http://hz.5i5j.com/community/gongshu/p1',
                     'http://hz.5i5j.com/community/xiacheng/p1',
                     'http://hz.5i5j.com/community/shangcheng/p1',
                     'http://hz.5i5j.com/community/binjiang/p1',
                     'http://hz.5i5j.com/community/yuhang/p1',
                     'http://hz.5i5j.com/community/xiaoshan/p1',
                     'http://hz.5i5j.com/community/xihu/p1',
                     'http://hz.5i5j.com/community/jianggan/p1',
                     'http://hz.5i5j.com/community/fuyang/p1',
                     'http://hz.5i5j.com/community/p2/',
                     'http://hz.5i5j.com/community/p3/',
                     'http://hz.5i5j.com/community/p4/',
                     'http://hz.5i5j.com/community/p5/',
                     'http://hz.5i5j.com/community/p6/',
                     'http://hz.5i5j.com/community/p7/']
#    start_urls = ['http://hz.5i5j.com/community/p7/']
    
    base_url = 'http://hz.5i5j.com'
    base_exchang_url = '/exchange/getdeals?communityId='

    def parse(self, response):
        maxnum = int(response.xpath("//div[@class='list-comm-l']/h3[@class='list-comm-sort']/font[@class='font-houseNum']/text()").extract()[0])
        if maxnum % 12 == 0:
            maxpage = maxnum/12
        else:
            maxpage = maxnum/12 + 1
        for page in range(1,maxpage+1):
            url = response.url + 'n' + str(page)
            yield Request(url,callback=self.get_blockid)
            
    def get_blockid(self,response):    
        block_list = response.xpath("//ul[@class='list-body']/li/div[@class='list-info-comm']/h2/a")
        block_num = []
        for block in block_list:
            item = WawjItem()
            block_name = block.xpath("./text()").extract()[0]
            item['blockname'] = block_name
            block_url = block.xpath("./@href").extract()[0]
            id = block_url.split('/')[2]
            block_url = self.base_url + block_url
            item['blockurl'] = block_url
            block_num.append(block_url)
            yield Request(block_url,callback=self.get_maxpage_block,dont_filter=True,meta={'item':item,'id':id,'block_num':block_num})

    def get_maxpage_block(self,response):       
        item = response.meta['item']
        id = response.meta['id']
        maxpage_block_lst = response.xpath("//ul[@class='deal-page']/a[last()-1]/li/text()").extract()
        if len(maxpage_block_lst) != 0:
            maxpage_block = int(maxpage_block_lst[0])
        else:
            maxpage_block = 1
        for page in range(1,maxpage_block+1):
            url = self.base_url + self.base_exchang_url + id + '&page=' + str(page)
            yield Request(url,dont_filter=True,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36','X-Requested-With':'XMLHttpRequest'},callback=self.get_info,meta={'item':item})
    

    def get_info(self,response):
        print 'Sell url: ',response.url
        item = response.meta['item']
        node_list = response.xpath("//ul[@class='watch-record-text2']")      
        if len(node_list) == 0:                  
            item['houseinfo'] = 'None'
            item['housearea'] = 'None'
            item['data'] = 'None'
            item['totalprice'] = 'None'
            item['unitprice'] = 'None'
            yield item
        else:
            for node in node_list:                    
                item['houseinfo'] = node.xpath("./li[1]/p[2]/b/text()").extract()[0]
                item['housearea'] = node.xpath("./li[2]/text()").extract()[0]
                item['data'] = node.xpath("./li[3]/text()").extract()[0]
                item['totalprice'] = node.xpath("./li[4]/text()").extract()[0]
                item['unitprice'] = node.xpath("./li[5]/text()").extract()[0]
                yield item 

定义peplines部分

import json
import pandas


class LianjiaPipeline(object):
    def __init__(self):
        self.f = open('c:\\test\\ceshi.json','w')
    
    
    def process_item(self, item, spider):
        content = json.dumps(dict(item),ensure_ascii=False)+'\n'
        self.f.write(content.encode('utf-8'))
        return item
    
    def close_spider(self,spider):
        self.f.close()

相关文章

  • 爬虫代码--我爱我家(小区成交)

    先放代码,具体过程明天再说。 Scrapy结构 定义item部分 Spider部分 定义peplines部分

  • 有关 webscraper 的一些问题

    《不用写代码的爬虫课》发布后,有不少朋友来找我抓数据,但是有的朋友对于爬虫不了解,所以造成交流不畅。这篇文章就是分...

  • webscraper 抓数据需要的信息

    《不用写代码的爬虫课》发布后,有不少朋友来找我抓数据,但是有的朋友对于爬虫不了解,所以造成交流不畅。这篇文章就是分...

  • 利用Scrapy爬取我爱我家的小区成交量

    这次换一个售房网站来练练手,爬取目标是我爱我家的成交记录。 那么,我们想要爬取的每条成交价格,实则在二级界面的详细...

  • 我的爬虫代码

    基于关键词爬百度百科 基于关键词用selenium爬百度百科

  • 我家的小区

    (三年级张静嘉文) 我家在江南丽苑,是个很美丽的地方。 这里的树木很美。微风一吹,花儿在树上跳舞...

  • 我家的小区

    我家的小区特别的美丽。 小区门口有一块大石头,上面刻着界纪名门四个字,过了石头就可以看到一排排整齐的树...

  • 我家的小区

    我家的小区特别美丽。 我家小区里有一个大圆盘,里面种满了花草树木,到了春天,小区的花全开了,有白的、红的、绿的、粉...

  • 我家的小区

    吃过晚饭,不愿动,坐在餐桌前刷手机。有人敲门,原来是辖区派出所民警入户走访,今天在业主群里已通知,走访的时间是早八...

  • 我家的小区

    我家的小区一年四季都很美。 春天,万物复苏,各种植物从栅栏里探出脑袋,真是“春色满园关不住,一枝红杏出...

网友评论

      本文标题:爬虫代码--我爱我家(小区成交)

      本文链接:https://www.haomeiwen.com/subject/wogsmxtx.html