自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Python告訴你深圳房租有多高

大數(shù)據(jù) 數(shù)據(jù)分析
最近各大一二線城市的房租都有上漲,究竟整體上漲到什么程度呢?我們也不得而知,于是乎 zone 為了一探究竟,便用 Python 爬取了房某下的深圳的租房數(shù)據(jù),以下是本次的樣本數(shù)據(jù)。

概述

  • 前言
  • 統(tǒng)計(jì)結(jié)果
  • 爬蟲技術(shù)分析
  • 爬蟲代碼實(shí)現(xiàn)
  • 爬蟲分析實(shí)現(xiàn)
  • 后記

前言

最近各大一二線城市的房租都有上漲,究竟整體上漲到什么程度呢?我們也不得而知,于是乎 zone 為了一探究竟,便用 Python 爬取了房某下的深圳的租房數(shù)據(jù),以下是本次的樣本數(shù)據(jù): 

用Python告訴你深圳房租有多高

樣本數(shù)據(jù)

 

除去【不限】的數(shù)據(jù)(因?yàn)榭赡軙c后面重疊),總數(shù)據(jù)量為 16971 ,其中后半部分地區(qū)數(shù)據(jù)量偏少,是由于該區(qū)房源確實(shí)不足。因此,此次調(diào)查也并非非常準(zhǔn)確,權(quán)且當(dāng)個娛樂項(xiàng)目,供大家觀賞。

統(tǒng)計(jì)結(jié)果

我們且先看統(tǒng)計(jì)結(jié)果,然后再看技術(shù)分析。

深圳房源分布:(按區(qū)劃分)

其中福田與南山的房源分布是最多的。但這兩塊地的房租可是不菲啊。 

用Python告訴你深圳房租有多高

房源分布

 

房租單價(jià):(每月每平方米單價(jià) -- 平均數(shù))

即是 1 平方米 1 個月的價(jià)格。方塊越大,代表價(jià)格越高。 

用Python告訴你深圳房租有多高

房租單價(jià):平方米/月

 

可以看出福田與南山是獨(dú)占鰲頭,分別是 114.874 與 113.483 ,是其他地區(qū)的幾倍。如果租個福田 20 平方的房間:

  • 114.874 x 20 = 2297.48

再來個兩百的水電、物業(yè):

  • 2297.48 + 200 = 2497.48

我們節(jié)儉一點(diǎn)來算的話,每天早餐 10 塊,中午 25 塊,晚飯 25 塊:

  • 2497.48 + 50 x 30 = 3997.48

是的,僅僅是活下來就需要 3997.48 塊。

隔斷時間下個館子,每個月買些衣服,交通費(fèi),談個女朋友,與女朋友出去逛街,妥妥滴加個 3500

  • 3997.48 + 3500 = 7497.48

給爸媽一人一千:

  • 7497.48 + 2000 = 9497.48

月薪一萬妥妥滴,變成了月光族。

房租單價(jià):(每日每平方米單價(jià) -- 平均數(shù))

  • 即是 1 平方米 1 天的價(jià)格。 

用Python告訴你深圳房租有多高

租房單價(jià):平方米/日

 

以前在鄉(xiāng)下沒有***的感覺,那么可以到北上廣深體驗(yàn)一下,福田區(qū)每平方米每天需要 3.829 元。[捂臉]

戶型

戶型主要以 3 室 2 廳與 2 室 2 廳為主。與小伙伴抱團(tuán)租房是***的選擇了,不然與不認(rèn)識的人一起合租,可能會發(fā)生一系列讓你不舒服的事情。字體越大,代表戶型數(shù)量越多。 

用Python告訴你深圳房租有多高

戶型

用Python告訴你深圳房租有多高

戶型

 

租房面積統(tǒng)計(jì)

其中 30 - 90 平方米的租房占大多數(shù),如今之計(jì),也只能是幾個小伙伴一起租房,抱團(tuán)取暖了。 

用Python告訴你深圳房租有多高

租房面積統(tǒng)計(jì)

 

租房描述詞云

這是爬取的租房描述,其中字體越大,標(biāo)識出現(xiàn)的次數(shù)越多。其中【精裝修】占據(jù)了很大的部分,說明長租公寓也占領(lǐng)了很大一部分市場。 

用Python告訴你深圳房租有多高

租房描述

 

爬蟲思路

先爬取房某下深圳各個板塊的數(shù)據(jù),然后存進(jìn) MongoDB 數(shù)據(jù)庫,***再進(jìn)行數(shù)據(jù)分析。 

用Python告訴你深圳房租有多高

各個板塊

 

數(shù)據(jù)庫部分?jǐn)?shù)據(jù): 

  1. /* 1 */  
  2.  
  3. "_id" : ObjectId("5b827d5e8a4c184e63fb1325"),  
  4. "traffic" : "距沙井電子城公交站約567米。",//交通描述  
  5. "address" : "寶安-沙井-名豪麗城",//地址  
  6. "price" : 3100,//價(jià)格  
  7. "area" : 110,//面積  
  8. "direction" : "朝南\r\n ",//朝向  
  9. "title" : "沙井 名豪麗城精裝三房 家私齊拎包住 高層朝南隨時看房",//標(biāo)題  
  10. "rooms" : "3室2廳",//戶型  
  11. "region" : "寶安"//地區(qū) 
  12.  

爬蟲技術(shù)分析

  • 請求庫:requests
  • HTML 解析:BeautifulSoup
  • 詞云:wordcloud
  • 數(shù)據(jù)可視化:pyecharts
  • 數(shù)據(jù)庫:MongoDB
  • 數(shù)據(jù)庫連接:pymongo

爬蟲代碼實(shí)現(xiàn)

首先右鍵網(wǎng)頁,查看頁面源碼,找出我們要爬取得部分。

用Python告訴你深圳房租有多高

源碼

代碼實(shí)現(xiàn),由于篇幅原因只展示主要代碼:(獲取一個頁面的數(shù)據(jù)) 

  1. def getOnePageData(self, pageUrl, reginon="不限"):  
  2. rent = self.getCollection(self.region)  
  3. self.session.headers.update({  
  4. 'User-Agent''Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'})  
  5. res = self.session.get(  
  6. pageUrl  
  7.  
  8. soup = BeautifulSoup(res.text, "html.parser" 
  9. divs = soup.find_all("dd", attrs={"class""info rel"}) # 獲取需要爬取得 div  
  10. for div in divs:  
  11. ps = div.find_all("p" 
  12. try: # 捕獲異常,因?yàn)轫撁嬷杏行?shù)據(jù)沒有被填寫完整,或者被插入了一條廣告,則會沒有相應(yīng)的標(biāo)簽,所以會報(bào)錯  
  13. for index, p in enumerate(ps): # 從源碼中可以看出,每一條 p 標(biāo)簽都有我們想要的信息,故在此遍歷 p 標(biāo)簽,  
  14. text = p.text.strip()  
  15. print(text) # 輸出看看是否為我們想要的信息  
  16. print("===================================" 
  17. # 爬取并存進(jìn) MongoDB 數(shù)據(jù)庫  
  18. roomMsg = ps[1].text.split("|" 
  19. # rentMsg 這樣處理是因?yàn)橛行┬畔⑽刺顚懲暾瑢?dǎo)致對象報(bào)空  
  20. area = roomMsg[2].strip()[:len(roomMsg[2]) - 2]  
  21. rentMsg = self.getRentMsg(  
  22. ps[0].text.strip(), 
  23.  roomMsg[1].strip(), 
  24.  int(float(area)),  
  25. int(ps[len(ps) - 1].text.strip()[:len(ps[len(ps) - 1].text.strip()) - 3]),  
  26. ps[2].text.strip(),  
  27. ps[3].text.strip(),  
  28. ps[2].text.strip()[:2],  
  29. roomMsg[3],  
  30.  
  31. rent.insert(rentMsg)  
  32. except 
  33. continue 

數(shù)據(jù)分析實(shí)現(xiàn)

數(shù)據(jù)分析: 

  1. # 求一個區(qū)的房租單價(jià)(平方米/元)  
  2. def getAvgPrice(self, region):  
  3. areaPinYin = self.getPinyin(region=region)  
  4. collection = self.zfdb[areaPinYin]  
  5. totalPrice = collection.aggregate([{'$group': {'_id''$region''total_price': {'$sum''$price'}}}])  
  6. totalArea = collection.aggregate([{'$group': {'_id''$region''total_area': {'$sum''$area'}}}])  
  7. totalPrice2 = list(totalPrice)[0]["total_price" 
  8. totalArea2 = list(totalArea)[0]["total_area" 
  9. return totalPrice2 / totalArea2  
  10. # 獲取各個區(qū) 每個月一平方米需要多少錢  
  11. def getTotalAvgPrice(self):  
  12. totalAvgPriceList = []  
  13. totalAvgPriceDirList = []  
  14. for index, region in enumerate(self.getAreaList()):  
  15. avgPrice = self.getAvgPrice(region)  
  16. totalAvgPriceList.append(round(avgPrice, 3))  
  17. totalAvgPriceDirList.append({"value": round(avgPrice, 3), "name": region + " " + str(round(avgPrice, 3))})  
  18. return totalAvgPriceDirList  
  19. # 獲取各個區(qū) 每一天一平方米需要多少錢  
  20. def getTotalAvgPricePerDay(self): 
  21.  totalAvgPriceList = []  
  22. for index, region in enumerate(self.getAreaList()):  
  23. avgPrice = self.getAvgPrice(region)  
  24. totalAvgPriceList.append(round(avgPrice / 30, 3))  
  25. return (self.getAreaList(), totalAvgPriceList)  
  26. # 獲取各區(qū)統(tǒng)計(jì)樣本數(shù)量  
  27. def getAnalycisNum(self):  
  28. analycisList = []  
  29. for index, region in enumerate(self.getAreaList()):  
  30. collection = self.zfdb[self.pinyinDir[region]]  
  31. print(region)  
  32. totalNum = collection.aggregate([{'$group': {'_id''''total_num': {'$sum': 1}}}])  
  33. totalNum2 = list(totalNum)[0]["total_num" 
  34. analycisList.append(totalNum2)  
  35. return (self.getAreaList(), analycisList)  
  36. # 獲取各個區(qū)的房源比重  
  37. def getAreaWeight(self):  
  38. result = self.zfdb.rent.aggregate([{'$group': {'_id''$region''weight': {'$sum': 1}}}])  
  39. areaName = [] 
  40.  areaWeight = []  
  41. for item in result:  
  42. if item["_id"in self.getAreaList(): 
  43. areaWeight.append(item["weight"])  
  44. areaName.append(item["_id"])  
  45. print(item["_id"])  
  46. print(item["weight"])  
  47. # print(type(item))  
  48. return (areaName, areaWeight)  
  49. # 獲取 title 數(shù)據(jù),用于構(gòu)建詞云 
  50.  def getTitle(self):  
  51. collection = self.zfdb["rent" 
  52. queryArgs = {}  
  53. projectionFields = {'_id'False'title'True} # 用字典指定需要的字段  
  54. searchRes = collection.find(queryArgs, projection=projectionFields).limit(1000)  
  55. content = ''  
  56. for result in searchRes:  
  57. print(result["title"])  
  58. content += result["title" 
  59. return content  
  60. # 獲取戶型數(shù)據(jù)(例如:3 室 2 廳)  
  61. def getRooms(self): 
  62.  results = self.zfdb.rent.aggregate([{'$group': {'_id''$rooms''weight': {'$sum': 1}}}])  
  63. roomList = []  
  64. weightList = [] 
  65.  for result in results:  
  66. roomList.append(result["_id"])  
  67. weightList.append(result["weight"])  
  68. # print(list(result))  
  69. return (roomList, weightList)  
  70. # 獲取租房面積  
  71. def getAcreage(self):  
  72. results0_30 = self.zfdb.rent.aggregate([ 
  73.  {'$match': {'area': {'$gt': 0, '$lte': 30}}},  
  74. {'$group': {'_id''''count': {'$sum': 1}}} 
  75.  ])  
  76. results30_60 = self.zfdb.rent.aggregate([  
  77. {'$match': {'area': {'$gt': 30, '$lte': 60}}},  
  78. {'$group': {'_id''''count': {'$sum': 1}}}  
  79. ])  
  80. results60_90 = self.zfdb.rent.aggregate([  
  81. {'$match': {'area': {'$gt': 60, '$lte': 90}}},  
  82. {'$group': {'_id''''count': {'$sum': 1}}}  
  83. ])  
  84. results90_120 = self.zfdb.rent.aggregate([  
  85. {'$match': {'area': {'$gt': 90, '$lte': 120}}}, 
  86.  {'$group': {'_id''''count': {'$sum': 1}}}  
  87. ]) 
  88.  results120_200 = self.zfdb.rent.aggregate([  
  89. {'$match': {'area': {'$gt': 120, '$lte': 200}}}, 
  90.  {'$group': {'_id''''count': {'$sum': 1}}}  
  91. ])  
  92. results200_300 = self.zfdb.rent.aggregate([  
  93. {'$match': {'area': {'$gt': 200, '$lte': 300}}},  
  94. {'$group': {'_id''''count': {'$sum': 1}}}  
  95. ])  
  96. results300_400 = self.zfdb.rent.aggregate([  
  97. {'$match': {'area': {'$gt': 300, '$lte': 400}}},  
  98. {'$group': {'_id''''count': {'$sum': 1}}}  
  99. ]) 
  100.  results400_10000 = self.zfdb.rent.aggregate([  
  101. {'$match': {'area': {'$gt': 300, '$lte': 10000}}},  
  102. {'$group': {'_id''''count': {'$sum': 1}}}  
  103. ])  
  104. results0_30_ = list(results0_30)[0]["count" 
  105. results30_60_ = list(results30_60)[0]["count" 
  106. results60_90_ = list(results60_90)[0]["count" 
  107. results90_120_ = list(results90_120)[0]["count" 
  108. results120_200_ = list(results120_200)[0]["count" 
  109. results200_300_ = list(results200_300)[0]["count" 
  110. results300_400_ = list(results300_400)[0]["count" 
  111. results400_10000_ = list(results400_10000)[0]["count" 
  112. attr = ["0-30平方米""30-60平方米""60-90平方米""90-120平方米""120-200平方米""200-300平方米""300-400平方米""400+平方米" 
  113. value = [  
  114. results0_30_, results30_60_, results60_90_, results90_120_, results120_200_, results200_300_, results300_400_, results400_10000_  
  115.  
  116. return (attr, value) 

數(shù)據(jù)展示: 

  1. # 展示餅圖  
  2. def showPie(self, title, attr, value):  
  3. from pyecharts import Pie  
  4. pie = Pie(title)  
  5. pie.add("aa", attr, value, is_label_show=True 
  6. pie.render()  
  7. # 展示矩形樹圖  
  8. def showTreeMap(self, title, data):  
  9. from pyecharts import TreeMap  
  10. data = data  
  11. treemap = TreeMap(title, width=1200, height=600)  
  12. treemap.add("深圳", data, is_label_show=True, label_pos='inside', label_text_size=19)  
  13. treemap.render()  
  14. # 展示條形圖  
  15. def showLine(self, title, attr, value):  
  16. from pyecharts import Bar  
  17. bar = Bar(title)  
  18. bar.add("深圳", attr, value, is_convert=False, is_label_show=True, label_text_size=18, is_random=True 
  19. # xaxis_interval=0, xaxis_label_textsize=9,  
  20. legend_text_size=18, label_text_color=["#000"])  
  21. bar.render()  
  22. # 展示詞云  
  23. def showWorkCloud(self, content, image_filename, font_filename, out_filename):  
  24. d = path.dirname(__name__)  
  25. # content = open(path.join(d, filename), 'rb').read()  
  26. # 基于TF-IDF算法的關(guān)鍵字抽取, topK返回頻率***的幾項(xiàng), 默認(rèn)值為20, withWeight  
  27. # 為是否返回關(guān)鍵字的權(quán)重  
  28. tags = jieba.analyse.extract_tags(content, topK=100, withWeight=False 
  29. text = " ".join(tags)  
  30. # 需要顯示的背景圖片  
  31. img = imread(path.join(d, image_filename))  
  32. # 指定中文字體, 不然會亂碼的  
  33. wc = WordCloud(font_path=font_filename,  
  34. background_color='black' 
  35. # 詞云形狀,  
  36. mask=img,  
  37. # 允許***詞匯  
  38. max_words=400,  
  39. # ***號字體,如果不指定則為圖像高度  
  40. max_font_size=100,  
  41. # 畫布寬度和高度,如果設(shè)置了msak則不會生效  
  42. # width=600,  
  43. # height=400,  
  44. margin=2,  
  45. # 詞語水平擺放的頻率,默認(rèn)為0.9.即豎直擺放的頻率為0.1  
  46. prefer_horizontal=0.9  
  47.  
  48. wc.generate(text)  
  49. img_color = ImageColorGenerator(img)  
  50. plt.imshow(wc.recolor(color_func=img_color))  
  51. plt.axis("off" 
  52. plt.show()  
  53. wc.to_file(path.join(d, out_filename))  
  54. # 展示 pyecharts 的詞云  
  55. def showPyechartsWordCloud(self, attr, value):  
  56. from pyecharts import WordCloud  
  57. wordcloud = WordCloud(width=1300, height=620)  
  58. wordcloud.add("", attr, value, word_size_range=[20, 100])  
  59. wordcloud.render() 

后記

最近還真是挺多事情發(fā)生的,房租的暴漲,其實(shí)是資本力量進(jìn)駐了租房市場。自如、蛋殼這些長租公寓,相互抬高房租價(jià)格,而且讓客戶簽第三方貸款協(xié)議,前期發(fā)展可能需要一點(diǎn)錢,但是到后期壟斷市場之后,只要住房剛需在,就不會賺不回錢。***,應(yīng)對外界條件的變動,我們還是應(yīng)該提升自己的硬實(shí)力,這樣才能提升自己的生存能力。

責(zé)任編輯:未麗燕 來源: 簡書
相關(guān)推薦

2018-09-14 14:53:58

編程語言Python網(wǎng)絡(luò)爬蟲

2017-11-27 10:53:00

大數(shù)據(jù)租房數(shù)據(jù)分析

2017-11-24 12:52:01

大數(shù)據(jù)數(shù)據(jù)分析房租

2018-12-20 11:50:46

Python房租分析

2018-09-21 11:58:59

大數(shù)據(jù)

2020-08-27 08:18:39

機(jī)器學(xué)習(xí)

2020-01-09 13:24:31

Python 開發(fā)編程語言

2021-09-06 11:57:30

Python

2021-04-27 09:33:41

Python北京景點(diǎn)

2021-09-26 20:22:58

5GAI技術(shù)

2019-09-29 08:50:01

Python IT旅游

2017-07-27 17:28:58

記賬 網(wǎng)利寶

2021-07-26 11:02:29

鄭州暴雨河南

2021-02-27 10:38:56

Python結(jié)構(gòu)數(shù)據(jù)

2022-05-17 08:41:41

協(xié)程I/O模式

2021-08-02 14:27:18

大數(shù)據(jù)殺熟人工智能網(wǎng)絡(luò)投訴

2016-03-21 14:33:23

JavaPython程序員

2022-05-30 08:21:17

Kafka數(shù)據(jù)傳遞

2020-09-14 14:20:41

網(wǎng)絡(luò)風(fēng)險(xiǎn)漏洞網(wǎng)絡(luò)攻擊

2017-05-31 15:27:54

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號