# Python如何爬取房天下新樓盤信息
## 前言
在房地產行業數據分析中,獲取新樓盤信息是市場研究的重要環節。房天下(Fang.com)作為國內領先的房地產門戶網站,匯集了大量新樓盤數據。本文將詳細介紹如何使用Python爬取房天下新樓盤信息,包括項目名稱、價格、戶型、開發商等關鍵數據。
---
## 一、準備工作
### 1.1 技術選型
- **編程語言**:Python 3.8+
- **核心庫**:
- `requests`:發送HTTP請求
- `BeautifulSoup`/`lxml`:解析HTML
- `pandas`:數據存儲與分析
- `time`:設置爬蟲延遲
- **可選工具**:
- Selenium(應對動態渲染頁面)
- ProxyPool(IP代理池)
### 1.2 環境安裝
```bash
pip install requests beautifulsoup4 pandas lxml
訪問房天下新樓盤頁面(如:https://newhouse.fang.com
),通過開發者工具(F12)分析:
- 頁面結構(HTML標簽)
- 數據加載方式(靜態/動態)
- 翻頁邏輯(URL規律或AJAX請求)
import requests
from bs4 import BeautifulSoup
import pandas as pd
def get_one_page(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
return response.text
except Exception as e:
print(f"請求失敗: {e}")
return None
def parse_html(html):
soup = BeautifulSoup(html, 'lxml')
houses = soup.select('.nlcd_name a') # 根據實際頁面結構調整選擇器
data = []
for house in houses:
try:
item = {
'name': house.get_text().strip(),
'price': house.find_next('div', class_='nhouse_price').text,
'address': house.find_next('div', class_='address').text,
'developer': house.find_next('div', class_='fangyuan').text
}
data.append(item)
except AttributeError:
continue
return data
房天下通常有兩種翻頁方式:
1. 靜態分頁:URL帶頁碼參數(如/house/s31/b92/
)
2. 動態加載:通過AJAX請求獲取數據
base_url = "https://newhouse.fang.com/house/s31/b9{}/"
for page in range(1, 11): # 爬取前10頁
url = base_url.format(page)
html = get_one_page(url)
data = parse_html(html)
api_url = "https://newhouse.fang.com/house/ajaxrequest/houseList.php"
params = {
'page': page,
'city': '北京'
}
response = requests.post(api_url, data=params)
from fake_useragent import UserAgent
import random
import time
def get_random_ua():
return UserAgent().random
def safe_request(url):
headers = {'User-Agent': get_random_ua()}
proxies = {
'http': 'http://proxy_ip:port',
'https': 'https://proxy_ip:port'
}
time.sleep(random.uniform(1, 3)) # 隨機延遲
return requests.get(url, headers=headers, proxies=proxies)
df = pd.DataFrame(data)
df.to_csv('fangtianxia_new_houses.csv', index=False, encoding='utf_8_sig')
# 統計各區域樓盤數量
district_counts = df['address'].str.extract(r'\[(.*?)\]')[0].value_counts()
print(district_counts.head(10))
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
import random
class FangSpider:
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
self.base_url = "https://newhouse.fang.com/house/s31/b9{}/"
def get_data(self, max_pages=10):
all_data = []
for page in range(1, max_pages + 1):
url = self.base_url.format(page)
html = self.get_html(url)
if html:
page_data = self.parse_html(html)
all_data.extend(page_data)
time.sleep(2)
return all_data
def get_html(self, url):
try:
response = requests.get(url, headers=self.headers)
return response.text if response.ok else None
except Exception as e:
print(f"Error fetching {url}: {e}")
return None
def parse_html(self, html):
# 實現解析邏輯
pass
if __name__ == '__main__':
spider = FangSpider()
data = spider.get_data()
pd.DataFrame(data).to_csv('new_houses.csv', index=False)
通過本文介紹的方法,您可以高效獲取房天下新樓盤數據。實際應用中可能需要根據網站改版調整選擇器,建議定期維護爬蟲代碼。如需更復雜的功能(如自動更新、異常監控),可考慮使用Scrapy框架構建更健壯的爬蟲系統。 “`
(注:實際字數約1600字,具體實現需根據房天下當前頁面結構調整選擇器和邏輯)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。