python 自动抓取分析房价数据——安居客版

本文介绍如何使用Python爬虫从安居客抓取西双版纳房价数据,结合requests、beautifulsoup4和pandas进行数据清洗、分析及可视化,包括最高、最低、平均房价、中位数和标准差的统计。最后分享了代码封装和命令行运行的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

中秋回家,顺便想将家里闲置的房子卖出去。第一次卖房,没经验,于是决定委托给中介。中介要我定个价。最近几年,房价是涨了不少,但是长期在外,也不了解行情。真要定个价,心里还没个数。网上零零散散看了下,没有个系统的感知。心想,身为一代码农,为何要用这种低效的方式去了解房价。于是,就有了今天这篇专栏,也是继上篇《python 自动抓取分析文章阅读量——掘金专栏版》json 爬虫的一个补充。这次要抓取的房价来自安居客,西双版纳房价数据(其他房产相关的垂直平台还未覆盖)。之所以说是上一篇的补充,因为,这次数据来自 html 。废话不多说,撸起袖子开始干。

1. 准备工作

1.1 用到技术

  • python3
  • requests: http 爬取 html
  • beautifulsoup4: 从 html 字符串中提取需要的数据
  • pandas: 分析,保存数据
  • matplotlib: 数据可视化分析

1.2 安装

如已安装,请跳过。

pip install requests
pip install beautifulsoup4
pip install pandas
pip install matplotlib

1.3 导入

import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

2. 页面分析

2.1 打开页面

使用 Chrome 浏览器打开页面 https://bannan.anjuke.com/community/?from=navigation

2.2 定位目标元素选择器

在开发者工具中,找到楼盘列表容器 dom 元素选择器。这里看到的是,id 为 list-content。记下此 id

2.3 详细了解目标元素 dom 结构

在开发者工具控制台(Console)中,输入 document.getElementById('list-content') 回车。逐次展开 dom 树,找到目标数据所在的元素。下图标注出了本文目标数据字段所在的 dom 元素:

  • name: 楼盘名称
  • price: 价格
  • address 地址
  • latitude: 地图位置 纬度
  • longitude: 地图位置 经度

2.4 http 请求头

为了模拟(伪装)用户访问页面,最重要的就是获取浏览器正常请求页面数据的 http 请求头,并在 requests 中设置一样的请求头。其中最重要的请求头部字段就是 user-agent 用户代理。它是服务端用来辨别用户当前访问的设备,操作系统版本,浏览器厂商等信息的重要依据。另外部分网站,也会设置 cookie 字段,存储用户本次访问的会话信息,其中可能也包含了数据访问的权限信息,这种情况下,为了能正确抓取到数据,就必须提供此字段。如果不想做那么多分析,可以简单粗暴的直接将整个 header 复制使用。

3. 抓取数据

3.1 根据分页和 cookie 生成 http 请求头

经过第 2 小节的分析,发现,http 请求头中包含了分页信息和 cookie 。因此,我需要提供一个函数,动态生成 headers 。

def get_headers(page, cookie):
    headers = {
   
        'authority': 'bannan.anjuke.com',
        'method': 'GET',
        'path': '/community/p{}/'.format(page),
        'scheme': 'https',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'cookie': cookie,
        'pragma': 'no-cache',
        'referer': 'https://bannan.anjuke.com/community/p1/',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
    }
    return headers

3.2 抓取分页 html

def get_html_by_page(page, cookie):
    headers = get_headers(page, cookie)
    url = 'https://bannan.anjuke.com/community/p{}/'.format(page)
    res = requests.get(url, headers=headers)
    if res.status_code != 200:
        print('页面不存在!')
        return None
    return res.text

3.3 使用 beautifulsoup 从 html 提取原始数据

def extract_data_from_html(html):
    soup = BeautifulSoup(html)
    list_content = soup.find(id="list-content")
    if not list_content:
        return None
    items = list_content.find_all('div', class_='li-itemmod')
    if len(items) == 0:
        return None
    return [extract_data(item) for item in items]

def extract_data(item):
    name = item.find_all(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值