scrapy框架实现爬虫项目演示

下面我利用scrapy框架实现一个爬虫程序爬取深圳市房地产数据
第一步:建立scrapy工程
1、启动终端,定位到桌面,开启安装scrapy框架的虚拟环境。输入scrapy startproject szhome建立scrapy项目szhome。
在这里插入图片描述
如图,建立项目成功。
2、建立爬虫文件。终端进入scrapy项目szhome下的spiders文件夹下,输入scrapy genspider page bol.szhome.com/创建爬虫。
在这里插入图片描述
3、用pycharm打开scrapy项目如下。
在这里插入图片描述
4、配置item。
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
5、配置pipelines。
在这里插入图片描述
6、编写爬虫程序

# -*- coding: utf-8 -*-
import scrapy
from szhome import items
import csv


class PageSpider(scrapy.Spider):
    name = 'page'
    allowed_domains = ['szhome.com']
    start_urls = ['http://bol.szhome.com/search.html?page='+str(num) for num in range(1,141)]

    def parse(self, response):
        #得到房屋的连接
        room_link_list = response.xpath('//div[@class="mianbox"]/a/@href').extract()
        print('********************************')
        print(room_link_list)
        print('********************************')
        for i in room_link_list:
            url = 'http://bol.szhome.com'+i.replace('..','')
            print(url)
            #根据连接得到房屋次详情页面
            yield scrapy.Request(url = url, callback=self.getItem)



    def getItem(self,response):
        #根据次详情页面得到主详情连接
        url = response.xpath('//div[@class="left cloum1Left"]/p[@class="fix"]/a/@href').extract_first()
        detail_url = 'http://bol.szhome.com'+url
        #到达主详情页
        yield scrapy.Request(url = detail_url,callback=self.detailPage)


    def detailPage(self,response):
        #从主详情页提取信息
        item = items.SzhomeItem()
        item['new_building_name'] = (response.xpath('//*[@id="detailNav"]/div/div/h1/a/text()').extract())[0].strip()
        item['new_reference_avg_price'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[1]/li[1]/text()').extract())[0].strip()[5:]
        item['new_main_room_style'] = response.xpath('//span[@class="blue-14"]/a/text()|//span[@class="blue-14"]/span/text()').extract_first().strip()
        item['new_property_style'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[1]/li[5]/text()').extract_first()).strip()[5:]
        item['new_item_feature'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[1]/li[7]/text()').extract())[0].strip()[5:]
        item['new_developer'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[1]/li[4]/text()').extract())[0].strip()[6:]
        item['new_item_addr'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[1]/li[6]/text()').extract())[0].strip()[5:]
        item['new_latest_opening'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[0].strip()[5:]
        item['new_get_room_data'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[2].strip()[5:]
        item['new_property_comp'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[4].strip()[5:]
        item['new_property_cost'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[6].strip()[4:]
        item['new_contain_n'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[8].strip()[4:]
        item['new_green_n'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[9].strip()[4:]
        item['new_area'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[1].strip()[5:]
        item['new_room_area'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[3].strip()[5:]
        item['new_room_source'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[5].strip()[4:]
        item['new_car_position'] = (response.xpath('/html/body/div[7]/div[1]/div[1]/ul[2]/li/text()').extract())[7].strip()[4:]
        item['new_traffic_status'] = (response.xpath('/html/body/div[7]/div[2]/div[2]/text()').extract())[0].strip()
        item['new_supporting_facilities'] = (response.xpath('//div[@class="lh26"]/text()').extract())[0].strip()

        yield item

7、配置setting文件。
在这里插入图片描述
8、终端进入项目文件夹下的spiders文件夹,输入scrapy crawl page,运行page.py文件开始爬取数据。
在这里插入图片描述
9、结束之后进入spiders文件夹下,发现生成了一个spiders\csv.csv文件,excel打开可以看到我们爬到的数据。共2800条房地产数据。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38226778/article/details/86366184