python爬取贝壳网二手房信息
时间: 2023-08-10 21:48:01 浏览: 76
好的,以下是一个简单的 Python 爬虫程序,可以用来爬取贝壳网的二手房信息:
```python
import requests
from bs4 import BeautifulSoup
url = "https://bj.ke.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"}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
house_list = soup.find_all('div', class_='info')
for house in house_list:
name = house.find('div', class_='title').a.text.strip()
price = house.find('div', class_='price').span.text
print(name, price)
```
这个程序使用 requests 库和 BeautifulSoup 库来爬取页面信息,并且使用了 User-Agent 头来模拟浏览器请求。然后,使用 find_all 方法找到页面中所有房屋信息的 div 标签,遍历每一个标签,获取其中的房屋名称和房屋价格,最后输出到控制台上。