目錄
- 一、查找數(shù)據(jù)所在位置:
- 二、確定數(shù)據(jù)存放位置:
- 三、獲取html數(shù)據(jù):
- 四、解析html,提取有用數(shù)據(jù):
一、查找數(shù)據(jù)所在位置:
打開(kāi)鏈家官網(wǎng),進(jìn)入二手房頁(yè)面,選取某個(gè)城市,可以看到該城市房源總數(shù)以及房源列表數(shù)據(jù)。
![](http://img.jbzj.com/file_images/article/202105/202152283653586.png?20214228375)
二、確定數(shù)據(jù)存放位置:
某些網(wǎng)站的數(shù)據(jù)是存放在html中,而有些卻api接口,甚至有些加密在js中,還好鏈家的房源數(shù)據(jù)是存放到html中:
![](http://img.jbzj.com/file_images/article/202105/202152283751869.png?202142283756)
三、獲取html數(shù)據(jù):
通過(guò)requests請(qǐng)求頁(yè)面,獲取每頁(yè)的html數(shù)據(jù)
# 爬取的url,默認(rèn)爬取的南京的鏈家房產(chǎn)信息
url = 'https://nj.lianjia.com/ershoufang/pg{}/'.format(page)
# 請(qǐng)求url
resp = requests.get(url, headers=headers, timeout=10)
四、解析html,提取有用數(shù)據(jù):
通過(guò)BeautifulSoup解析html,并提取相應(yīng)有用的數(shù)據(jù)
soup = BeautifulSoup(resp.content, 'lxml')
# 篩選全部的li標(biāo)簽
sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')
# 循環(huán)遍歷
for sell in sellListContent:
# 標(biāo)題
title = sell.select('div.title a')[0].string
# 先抓取全部的div信息,再針對(duì)每一條進(jìn)行提取
houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)
# 樓盤(pán)名字
loupan = houseInfo[0]
# 對(duì)樓盤(pán)的信息進(jìn)行分割
info = houseInfo[0].split('|')
# 房子類(lèi)型
house_type = info[1].strip()
# 面積大小
area = info[2].strip()
# 房間朝向
toward = info[3].strip()
# 裝修類(lèi)型
renovation = info[4].strip()
# 房屋地址
positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))
# 房屋總價(jià)
totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))
# 房屋單價(jià)
unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]
以上就是我的分享,如果有什么不足之處請(qǐng)指出,多交流,謝謝!
以上就是python爬取鏈家二手房的數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python爬取鏈家二手房的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- Python手拉手教你爬取貝殼房源數(shù)據(jù)的實(shí)戰(zhàn)教程
- Python scrapy爬取蘇州二手房交易數(shù)據(jù)
- Python爬蟲(chóng)之爬取我愛(ài)我家二手房數(shù)據(jù)
- Python爬蟲(chóng)之爬取二手房信息
- 基于python爬取鏈家二手房信息代碼示例
- python爬蟲(chóng) 爬取58同城上所有城市的租房信息詳解
- Python爬蟲(chóng)入門(mén)案例之爬取二手房源數(shù)據(jù)