校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Python scrapy爬取蘇州二手房交易數據

Python scrapy爬取蘇州二手房交易數據

熱門標簽:洪澤縣地圖標注 大連crm外呼系統(tǒng) 北京電信外呼系統(tǒng)靠譜嗎 百度地圖標注位置怎么修改 無錫客服外呼系統(tǒng)一般多少錢 地圖標注視頻廣告 老人電話機器人 高德地圖標注是免費的嗎 梅州外呼業(yè)務系統(tǒng)

一、項目需求

使用Scrapy爬取鏈家網中蘇州市二手房交易數據并保存于CSV文件中
要求:
房屋面積、總價和單價只需要具體的數字,不需要單位名稱。
刪除字段不全的房屋數據,如有的房屋朝向會顯示“暫無數據”,應該剔除。
保存到CSV文件中的數據,字段要按照如下順序排列:房屋名稱,房屋戶型,建筑面積,房屋朝向,裝修情況,有無電梯,房屋總價,房屋單價,房屋產權。

二、項目分析

流程圖


通過控制臺發(fā)現所有房屋信息都在一個ul中其中每一個li里存儲一個房屋的信息。

找了到需要的字段,這里以房屋名稱為例,博主用linux截圖,沒法對圖片進行標注,這一段就是最中間的“景山玫瑰園” 。
其他字段類似不再一一列舉。
獲取了需要的數據后發(fā)現沒有電梯的配備情況,所以需要到詳細頁也就是點擊標題后進入的頁面,
點擊標題

可以看到里面有下需要的信息。

抓取詳細頁url

進行詳細頁數據分析

找到相應的位置,進行抓取數據。

三、編寫程序

創(chuàng)建項目,不說了。

1.編寫item(數據存儲)

import scrapy
class LianjiaHomeItem(scrapy.Item):
     name = scrapy.Field() # 名稱
     type = scrapy.Field()  # 戶型
     area = scrapy.Field()  # 面積
     direction = scrapy.Field()  #朝向
     fitment = scrapy.Field()  # 裝修情況
     elevator = scrapy.Field()  # 有無電梯
     total_price = scrapy.Field()  # 總價
     unit_price = scrapy.Field()  # 單價

2.編寫spider(數據抓取)

from scrapy import Request
from scrapy.spiders import Spider
from lianjia_home.items import LianjiaHomeItem

class HomeSpider(Spider):
    name = "home"
    current_page=1 #起始頁

    def start_requests(self): #初始請求
        url="https://su.lianjia.com/ershoufang/"
        yield Request(url=url)

    def parse(self, response): #解析函數
        list_selctor=response.xpath("http://li/div[@class='info clear']")
        for one_selector in list_selctor:
            try:
                #房屋名稱
                name=one_selector.xpath("http://div[@class='flood']/div[@class='positionInfo']/a/text()").extract_first()
                #其他信息
                other=one_selector.xpath("http://div[@class='address']/div[@class='houseInfo']/text()").extract_first()
                other_list=other.split("|")
                type=other_list[0].strip(" ")#戶型
                area = other_list[1].strip(" ") #面積
                direction=other_list[2].strip(" ") #朝向
                fitment=other_list[3].strip(" ") #裝修
                price_list=one_selector.xpath("div[@class='priceInfo']//span/text()")
                # 總價
                total_price=price_list[0].extract()
                # 單價
                unit_price=price_list[1].extract()

                item=LianjiaHomeItem()
                item["name"]=name.strip(" ")
                item["type"]=type
                item["area"] = area
                item["direction"] = direction
                item["fitment"] = fitment
                item["total_price"] = total_price
                item["unit_price"] = unit_price

            #生成詳細頁
                url = one_selector.xpath("div[@class='title']/a/@href").extract_first()
                yield Request(url=url,
                              meta={"item":item}, #把item作為數據v傳遞
                              callback=self.property_parse) #爬取詳細頁
            except:
                print("error")

        #獲取下一頁
            self.current_page+=1
            if self.current_page=100:
                next_url="https://su.lianjia.com/ershoufang/pg%d"%self.current_page
                yield Request(url=next_url)


    def property_parse(self,response):#詳細頁
        #配備電梯
        elevator=response.xpath("http://div[@class='base']/div[@class='content']/ul/li[last()]/text()").extract_first()
        item=response.meta["item"]
        item["elevator"]=elevator
        yield item

3.編寫pipelines(數據處理)

import re
from scrapy.exceptions import DropItem
class LianjiaHomePipeline:#數據的清洗
    def process_item(self, item, spider):
        #面積
        item["area"]=re.findall("\d+\.?\d*",item["area"])[0] #提取數字并存儲
        #單價
        item["unit_price"] = re.findall("\d+\.?\d*", item["unit_price"])[0] #提取數字并存儲

        #如果有不完全的數據,則拋棄
        if item["direction"] =="暫無數據":
            raise DropItem("無數據,拋棄:%s"%item)

        return item

class CSVPipeline(object):
    file=None
    index=0 #csv文件行數判斷
    def open_spider(self,spider): #爬蟲開始前,打開csv文件
        self.file=open("home.csv","a",encoding="utf=8")

    def process_item(self, item, spider):#按要求存儲文件。
        if self.index ==0:
            column_name="name,type,area,direction,fitment,elevator,total_price,unit_price\n"
            self.file.write(column_name)#插入第一行的索引信息
            self.index=1

        home_str=item["name"]+","+item["type"]+","+item["area"]+","+item["direction"]+","+item["fitment"]+","+item["elevator"]+","+item["total_price"]+","+item["unit_price"]+"\n"
        self.file.write(home_str) #插入獲取的信息

        return item

    def close_soider(self,spider):#爬蟲結束后關閉csv
        self.file.close()

4.編寫settings(爬蟲設置)

這里只寫下需要修改的地方

USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36'
#為裝成瀏覽器
ROBOTSTXT_OBEY = False #不遵循robots協(xié)議
ITEM_PIPELINES = {
    'lianjia_home.pipelines.LianjiaHomePipeline': 300,
    #先進行數字提取
    'lianjia_home.pipelines.CSVPipeline': 400
    #在進行數據的儲存
    #執(zhí)行順序由后邊的數字決定
}

這些內容在settings有些是默認關閉的,把用來注釋的 # 去掉即可開啟。

5.編寫start(代替命令行)

from scrapy import cmdline

cmdline.execute("scrapy crawl home" .split())

附上兩張結果圖。

總結

此次項目新增了簡單的數據清洗,在整體的數據抓取上沒有增加新的難度。

到此這篇關于Python scrapy爬取蘇州二手房交易數據的文章就介紹到這了,更多相關scrapy爬取二手房交易數據內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python手拉手教你爬取貝殼房源數據的實戰(zhàn)教程
  • Python爬蟲之爬取我愛我家二手房數據
  • python爬取鏈家二手房的數據
  • Python爬蟲之爬取二手房信息
  • 基于python爬取鏈家二手房信息代碼示例
  • python爬蟲 爬取58同城上所有城市的租房信息詳解
  • Python爬蟲入門案例之爬取二手房源數據

標簽:泉州 怒江 安慶 清遠 長春 洛陽 吉林 岳陽

巨人網絡通訊聲明:本文標題《Python scrapy爬取蘇州二手房交易數據》,本文關鍵詞  Python,scrapy,爬取,蘇州,二手房,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python scrapy爬取蘇州二手房交易數據》相關的同類信息!
  • 本頁收集關于Python scrapy爬取蘇州二手房交易數據的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 咸丰县| 望奎县| 阳春市| 平昌县| 黄浦区| 郎溪县| 桂平市| 潞西市| 滕州市| 泉州市| 西和县| 宜宾县| 天水市| 安图县| 麻栗坡县| 东丰县| 玛曲县| 吉木萨尔县| 长宁区| 平谷区| 饶平县| 新河县| 通榆县| 乌兰浩特市| 长兴县| 绿春县| 永寿县| 济宁市| 永善县| 黄浦区| 广东省| 元江| 娱乐| 鞍山市| 延庆县| 深泽县| 从化市| 竹北市| 马龙县| 铜鼓县| 察雅县|