Python scrapy爬取蘇州二手房交易數(shù)據(jù)
一、項(xiàng)目需求
使用Scrapy爬取鏈家網(wǎng)中蘇州市二手房交易數(shù)據(jù)并保存于CSV文件中
要求:
房屋面積、總價(jià)和單價(jià)只需要具體的數(shù)字,不需要單位名稱。
刪除字段不全的房屋數(shù)據(jù),如有的房屋朝向會(huì)顯示“暫無數(shù)據(jù)”,應(yīng)該剔除。
保存到CSV文件中的數(shù)據(jù),字段要按照如下順序排列:房屋名稱,房屋戶型,建筑面積,房屋朝向,裝修情況,有無電梯,房屋總價(jià),房屋單價(jià),房屋產(chǎn)權(quán)。
二、項(xiàng)目分析
流程圖
通過控制臺(tái)發(fā)現(xiàn)所有房屋信息都在一個(gè)ul中其中每一個(gè)li里存儲(chǔ)一個(gè)房屋的信息。
找了到需要的字段,這里以房屋名稱為例,博主用linux截圖,沒法對(duì)圖片進(jìn)行標(biāo)注,這一段就是最中間的“景山玫瑰園” 。
其他字段類似不再一一列舉。
獲取了需要的數(shù)據(jù)后發(fā)現(xiàn)沒有電梯的配備情況,所以需要到詳細(xì)頁也就是點(diǎn)擊標(biāo)題后進(jìn)入的頁面,
點(diǎn)擊標(biāo)題
可以看到里面有下需要的信息。
抓取詳細(xì)頁url
進(jìn)行詳細(xì)頁數(shù)據(jù)分析
找到相應(yīng)的位置,進(jìn)行抓取數(shù)據(jù)。
三、編寫程序
創(chuàng)建項(xiàng)目,不說了。
1.編寫item(數(shù)據(jù)存儲(chǔ))
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() # 總價(jià) unit_price = scrapy.Field() # 單價(jià)
2.編寫spider(數(shù)據(jù)抓取)
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): #初始請(qǐng)求 url="https://su.lianjia.com/ershoufang/" yield Request(url=url) def parse(self, response): #解析函數(shù) 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()") # 總價(jià) total_price=price_list[0].extract() # 單價(jià) 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 #生成詳細(xì)頁 url = one_selector.xpath("div[@class='title']/a/@href").extract_first() yield Request(url=url, meta={"item":item}, #把item作為數(shù)據(jù)v傳遞 callback=self.property_parse) #爬取詳細(xì)頁 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):#詳細(xì)頁 #配備電梯 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(數(shù)據(jù)處理)
import re from scrapy.exceptions import DropItem class LianjiaHomePipeline:#數(shù)據(jù)的清洗 def process_item(self, item, spider): #面積 item["area"]=re.findall("\d+\.?\d*",item["area"])[0] #提取數(shù)字并存儲(chǔ) #單價(jià) item["unit_price"] = re.findall("\d+\.?\d*", item["unit_price"])[0] #提取數(shù)字并存儲(chǔ) #如果有不完全的數(shù)據(jù),則拋棄 if item["direction"] =="暫無數(shù)據(jù)": raise DropItem("無數(shù)據(jù),拋棄:%s"%item) return item class CSVPipeline(object): file=None index=0 #csv文件行數(shù)判斷 def open_spider(self,spider): #爬蟲開始前,打開csv文件 self.file=open("home.csv","a",encoding="utf=8") def process_item(self, item, spider):#按要求存儲(chǔ)文件。 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):#爬蟲結(jié)束后關(guān)閉csv self.file.close()
4.編寫settings(爬蟲設(shè)置)
這里只寫下需要修改的地方
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, #先進(jìn)行數(shù)字提取 'lianjia_home.pipelines.CSVPipeline': 400 #在進(jìn)行數(shù)據(jù)的儲(chǔ)存 #執(zhí)行順序由后邊的數(shù)字決定 }
這些內(nèi)容在settings有些是默認(rèn)關(guān)閉的,把用來注釋的 # 去掉即可開啟。
5.編寫start(代替命令行)
from scrapy import cmdline cmdline.execute("scrapy crawl home" .split())
附上兩張結(jié)果圖。
總結(jié)
此次項(xiàng)目新增了簡(jiǎn)單的數(shù)據(jù)清洗,在整體的數(shù)據(jù)抓取上沒有增加新的難度。
到此這篇關(guān)于Python scrapy爬取蘇州二手房交易數(shù)據(jù)的文章就介紹到這了,更多相關(guān)scrapy爬取二手房交易數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python網(wǎng)絡(luò)編程之Socket和HTTP協(xié)議詳解
在當(dāng)今互聯(lián)網(wǎng)時(shí)代,網(wǎng)絡(luò)編程是程序員不可或缺的一項(xiàng)技能,本文將介紹Python中兩種主要的網(wǎng)絡(luò)編程方式:Socket編程和基于HTTP協(xié)議的網(wǎng)絡(luò)編程,希望對(duì)大家有所幫助2024-04-04python中數(shù)組nums[:]和nums的區(qū)別
本文主要介紹了python中數(shù)組nums[:]和nums的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07使用Python為Excel文件添加預(yù)設(shè)和自定義文檔屬性
向Excel文件添加文檔屬性是專業(yè)地組織和管理電子表格數(shù)據(jù)的關(guān)鍵步驟,這些屬性,如標(biāo)題、作者、主題和關(guān)鍵詞,增強(qiáng)了文件的元數(shù)據(jù),使得在大型數(shù)據(jù)庫或文件系統(tǒng)中跟蹤變得更加容易,本文將介紹如何使用Python高效地為Excel文件添加文檔屬性,需要的朋友可以參考下2024-05-05詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化
這篇文章主要介紹了Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Centos 升級(jí)到python3后pip 無法使用的解決方法
今天小編就為大家分享一篇Centos 升級(jí)到python3后pip 無法使用的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python基于Tkinter開發(fā)一個(gè)爬取B站直播彈幕的工具
這篇文章主要介紹了Python Tkinter如何開發(fā)一個(gè)爬取B站直播彈幕的工具,幫助大家更好的利用python進(jìn)行圖形界面的開發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-05-05Python自定義函數(shù)的創(chuàng)建、調(diào)用和函數(shù)的參數(shù)詳解
這篇文章主要介紹了Python自定義函數(shù)的創(chuàng)建、調(diào)用和函數(shù)的參數(shù)、變量作用域等常見問題,需要的朋友可以參考下2014-03-03