python爬取鏈家二手房的數(shù)據(jù)
打開鏈家官網(wǎng),進(jìn)入二手房頁面,選取某個城市,可以看到該城市房源總數(shù)以及房源列表數(shù)據(jù)。
某些網(wǎng)站的數(shù)據(jù)是存放在html中,而有些卻api接口,甚至有些加密在js中,還好鏈家的房源數(shù)據(jù)是存放到html中:
通過requests請求頁面,獲取每頁的html數(shù)據(jù)
# 爬取的url,默認(rèn)爬取的南京的鏈家房產(chǎn)信息url = ’https://nj.lianjia.com/ershoufang/pg{}/’.format(page)# 請求urlresp = requests.get(url, headers=headers, timeout=10)四、解析html,提取有用數(shù)據(jù):
通過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信息,再針對每一條進(jìn)行提取 houseInfo = list(sell.select(’div.houseInfo’)[0].stripped_strings) # 樓盤名字 loupan = houseInfo[0] # 對樓盤的信息進(jìn)行分割 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)) # 房屋總價(jià) totalPrice = ’’.join(list(sell.select(’div.totalPrice’)[0].stripped_strings)) # 房屋單價(jià) unitPrice = list(sell.select(’div.unitPrice’)[0].stripped_strings)[0]
以上就是我的分享,如果有什么不足之處請指出,多交流,謝謝!
以上就是python爬取鏈家二手房的數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python爬取鏈家二手房的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Java Swing權(quán)威指南:Spinner Model Controls2. PHP里10個鮮為人知但卻非常有用的函數(shù)3. layui的checbox在Ajax局部刷新下的設(shè)置方法4. Vue如何提升首屏加載速度實(shí)例解析5. Android Studio 利用Splash制作APP啟動界面的方法6. IntelliJ IDEA設(shè)置編碼格式的方法7. asp中response.write("中文")或者js中文亂碼問題8. django列表篩選功能的實(shí)現(xiàn)代碼9. 詳解盒子端CSS動畫性能提升10. JavaWeb Servlet中url-pattern的使用
