目錄
- 一、查找數據所在位置:
- 二、確定數據存放位置:
- 三、獲取html數據:
- 四、解析html,提取有用數據:
一、查找數據所在位置:
打開鏈家官網,進入二手房頁面,選取某個城市,可以看到該城市房源總數以及房源列表數據。

二、確定數據存放位置:
某些網站的數據是存放在html中,而有些卻api接口,甚至有些加密在js中,還好鏈家的房源數據是存放到html中:

三、獲取html數據:
通過requests請求頁面,獲取每頁的html數據
# 爬取的url,默認爬取的南京的鏈家房產信息
url = 'https://nj.lianjia.com/ershoufang/pg{}/'.format(page)
# 請求url
resp = requests.get(url, headers=headers, timeout=10)
四、解析html,提取有用數據:
通過BeautifulSoup解析html,并提取相應有用的數據
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爬取鏈家二手房的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- Python手拉手教你爬取貝殼房源數據的實戰教程
- Python scrapy爬取蘇州二手房交易數據
- Python爬蟲之爬取我愛我家二手房數據
- Python爬蟲之爬取二手房信息
- 基于python爬取鏈家二手房信息代碼示例
- python爬蟲 爬取58同城上所有城市的租房信息詳解
- Python爬蟲入門案例之爬取二手房源數據