貧窮限制了個人想象,從大學進入到社會這麼久,從剛開始的興致勃勃,以爲錢有什麼難賺,到如今的啪啪打臉,就很真實,租房如今更是人生大事,在這擁擠的城市,都想先擁有一個屬於本身的空間,今天小編就來爬取鏈家二手房的數據,既能達到省錢又能免了不少去看房的時間,提早了解二手房的數據,不入坑,不進坑。html
打開鏈家官網,進入二手房頁面,選取某個城市,能夠看到該城市房源總數以及房源列表數據。web
某些網站的數據是存放在html中,而有些卻api接口,甚至有些加密在js中,還好鏈家的房源數據是存放到html中:api
經過requests請求頁面,獲取每頁的html數據markdown
# 爬取的url,默認爬取的南京的鏈家房產信息
url = 'https://nj.lianjia.com/ershoufang/pg{}/'.format(page)
# 請求url
resp = requests.get(url, headers=headers, timeout=10)
複製代碼
經過BeautifulSoup解析html,並提取相應有用的數據oop
soup = BeautifulSoup(resp.content, 'lxml')
# 篩選所有的li標籤
sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')
# 循環遍歷
for sell in sellListContent:
# 標題
title = sell.select('div.title a')[0].string
# 先抓取所有的div信息,再針對每一條進行提取
houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)
# 樓盤名字
loupan = houseInfo[0]
# 對樓盤的信息進行分割
info = houseInfo[0].split('|')
# 房子類型
house_type = info[1].strip()
# 面積大小
area = info[2].strip()
# 房間朝向
toward = info[3].strip()
# 裝修類型
renovation = info[4].strip()
# 房屋地址
positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))
# 房屋總價
totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))
# 房屋單價
unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]
複製代碼
但願給在他鄉由於租房迷茫的小夥伴有幫助,學習Python不只能夠幫助咱們的工做,對於生活上也有不少便利。學習
點擊領取學習資源 (●’◡’●)ノ♥
Python解答,遇到問題不要慌,來這裏~
基礎學習資料,數據分析,爬蟲等視頻,來這裏~\網站