没想到传了那么久的三孩生育政策真的来了!
刚说完,拥有三孩的大S和汪小菲就被传言闹离婚?
这几天,三孩生育政策一直是热门话题,网友们纷纷表示需要早早购置一下床铺:
图源网络
三个孩子的重担必然对房子的需求变得更大,如何选择一个合适的房子是一个大难题了。
然而对于未婚未育的黄帮主来说,暂时还考虑不到这个问题。
但是,没有孩子不代表没有房子问题。
现在,租房才是黄帮主最头疼的问题,又想便宜又想靠谱,最好离家近,地铁四通八达,房子阳光好。
这样的房子上哪儿去找呢?
这里给大家推荐一个好方法:提高预算(不是。
我们打工仔本来房租就是占每月支出的很大一部分了,那自然是不降低生活幸福感的情况下能省则省。
刚巧最近黄帮主准备换房子,自己利用python分析了一波南京的租房信息。
今天我们把黄帮主的租房攻略分享给大家,教大家如何用 Python自己分析房源。
不管租房,还是为了买大房子(羡慕),都非常适用哦!
01
数据获取
首先,必然是需要获取租房信息,没有信息的数据分析就仿佛苍蝇趴在玻璃上——前途一片光明,却走投无路。
下面我们来看看怎么获取数据吧。
本文数据来源为安居客,用python的requests和bs4库进行爬取页面,并解析,将结果写入csv文件中。
import requests,time,random
from bs4 import BeautifulSoup
import csv
defget_content(url):
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
}
response = requests.get(url,headers= headers)
soup = BeautifulSoup(response.text, "lxml")
zu_soups = soup.find_all("div",class_="zu-itemmod")
zufang_info = {}
with open("njzf.csv", "a", newline="", encoding="utf-8") as f:
for zu_soup in zu_soups:
zu_infos = zu_soup.find_all("div",class_="zu-info")
price = zu_soup.find("div",class_="zu-side").text.strip()
for zu_info in zu_infos:
title = zu_info.find("h3").text.strip()
layout = zu_info.find("p",class_="details-item tag").text.split("|")[0].strip()
area = zu_info.find("p", class_="details-item tag").text.split("|")[1].strip()
floor = zu_info.find("p", class_="details-item tag").text.split("|")[2].split("层")[0]
address = zu_info.find("address").text.strip()
address =" ".join(address.split())
rent_type = zu_info.find("p",class_="details-item bot-tag").find_all("span")[0].text
direction = zu_info.find("p", class_="details-item bot-tag").find_all("span")[1].text
try:
subway = zu_info.find("p", class_="details-item bot-tag").find("span", class_="cls-3").text
except:
subway =""
zufang_info["标题"] = title
zufang_info["布局"] = layout
zufang_info["面积"] = area
zufang_info["楼层"] = floor
zufang_info["地址"] = address
zufang_info["出租方式"] = rent_type
zufang_info["朝向"] = direction
zufang_info["地铁"] = subway
zufang_info["租金"] = price
table_row = [title,layout,area,floor,address,rent_type,direction,subway,price]
writer = csv.writer(f)
writer.writerow(table_row)
print(zufang_info)
return
if __name__=="__main__":
with open("njzf.csv", "a",newline="", encoding="utf-8") as f:
writer = csv.writer(f)
table_head = ["标题","布局","面积","楼层","地址","出租方式","朝向","地铁","租金"]
writer.writerow(table_head)
for i in range(0,50):
url ="https://nj.zu.anjuke.com/fangyuan/p{}/".format(i +1)
time.sleep(random.randint(1, 6))
get_content(url)
上下滑动
一直爬啊爬,共爬取了3000条有效租房数据,如下图所示,一看这个数量,心里是不是美滋滋,然而看了看自己的钱包,立马哭了,筛选下来没几条符合条件的。
02整体租房价格情况
大家一定是希望找到一个便宜的房子,黄帮主也是这么想的,那我们就统计下价格,筛选下自己心仪的价格;
def draw_histogram(x):
x_label = x.name
x = x.dropna()
x = np.array(x)
mu =round(x.mean(), 2)
sigma =round(x.std(), 2)
bins_num =10
n, bins, patches = plt.hist(x, bins_num, density=1, facecolor="blue", alpha=0.5)
y = norm.pdf(bins, mu, sigma)
plt.plot(bins, y, "r--")
plt.xlabel(x_label)
plt.ylabel("概率密度")
plt.title(r"{}正态分布: $mu={}$,$sigma={}$".format(x_label, mu, sigma))
plt.show()
我们再来看看图,选取租金一列,进行作图
f =open("njzf.csv", encoding="utf-8")
data = pd.read_csv(f)
f.close()
price = data["租金"].str.rstrip("元/月").astype("float")
如图所示
考虑单位面积的情况,对单位面积的租金再进行分析
area = data["面积"].str.rstrip("平米").astype("float")
price_per_m = price/area
data["单位面积租金"] = price_per_m
draw_histogram(data["单位面积租金"])
如下图
单位面积租金较单纯考虑租金更具有代表意义,从图中可以看出,南京租房平均每平方米需要46元/月。
03
数据分析
了解了整体情况后,我们首先开始按地区进行分析:
district = data["地址"].str.split(" ").str[1].str.split("-").str[0]
bars = plt.bar(district.value_counts().index, district.value_counts().values, color=["r", "g", "b", "m", "k", "y", "cyan", "gold"])
values =list(district.value_counts().values)
plt.title("各区房源数量")
for bar, value inzip(bars, values):
plt.text(bar.get_x()+0.5*bar.get_width(),bar.get_height(), value, ha="center", va="bottom")
plt.show()
可以看出,各区在安居客上的租房信息条数从高到低如上图所示,建邺有着最多的租房信息,其次是浦口和江宁。
分析得出各区房源后,下面就对租房的布局进行分析:
district = data["地址"].str.split(" ").str[1].str.split("-").str[0]
bars = plt.bar(district.value_counts().index, district.value_counts().values, color=["r", "g", "b", "m", "k", "y", "cyan", "gold"])
values =list(district.value_counts().values)
plt.title("各区房源数量")
for bar, value inzip(bars, values):
plt.text(bar.get_x()+0.5*bar.get_width(),bar.get_height(), value, ha="center", va="bottom")
plt.show()
从这个花花绿绿的图上可以看到:
59.47%是属于3室1厅型,最大的可能性是三个人合租的;其次是1室1厅,基本就是一个人或者情侣、夫妻这种;
再就是3室2厅,属于家庭型居住了。
04
总结
通过这一系列的分析,我们可以清晰的从房价,区域和房型几个方面筛选,找出最适合自己的房子。
这么详细的干货,你学会了么?赶快自己也试试看,租房买房不用愁!
﹀
﹀
﹀
以上就是这周的干货分享,如果想继续看干货,还想让大佬们输出更多技巧赶快关注并转发我们吧。点击“关注”,即可第一时间阅读每周干货哦!
#用扇贝玩编程,既高效又有趣