python爬取贝壳二手房
时间: 2023-11-02 13:06:03 浏览: 178
您可以使用Python来爬取贝壳网站上的二手房信息。下面是一个基本的爬取过程示例:
```python
import requests
from bs4 import BeautifulSoup
# 构造请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
# 发送请求并获取网页内容
url = 'https://www.ke.com/city/'
response = requests.get(url, headers=headers)
# 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
houses = soup.find_all('div', class_='info')
for house in houses:
# 提取房屋信息
title = house.find('a', class_='title').text.strip()
location = house.find('a', class_='location').text.strip()
price = house.find('span', class_='num').text.strip()
print(f'Title: {title}')
print(f'Location: {location}')
print(f'Price: {price}')
print('---')
```
阅读全文