python爬取链家二手房
时间: 2023-12-21 22:32:18 浏览: 92
以下是使用Python爬取链家二手房信息的示例代码:
```python
import requests
from bs4 import BeautifulSoup
import csv
# 发送HTTP请求获取页面内容
url = 'https://bj.lianjia.com/ershoufang/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
html = response.text
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, 'html.parser')
house_list = soup.find_all('div', class_='info clear')
# 保存数据到CSV文件
with open('二手房信息.csv', mode='a', encoding='utf-8-sig', newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['标题', '开发商', '房子信息', '发布周期', '售价/万', '单价'])
for house in house_list:
title = house.find('div', class_='title').a.text.strip()
developer = house.find('div', class_='houseInfo').text.strip()
info = house.find('div', class_='positionInfo').text.strip()
days = house.find('div', class_='followInfo').text.strip()
price = house.find('div', class_='totalPrice').span.text.strip()
unit_price = house.find('div', class_='unitPrice').span.text.strip()
csv_writer.writerow([title, developer, info, days, price, unit_price])
print("爬取完成并保存到CSV文件中。")
```
阅读全文