• 【python】爬取杭州市二手房销售数据做数据分析【附源码】


     一、背景

            在数据分析和市场调研中,获取房地产数据是至关重要的一环。本文介绍了如何利用 Python 中的 requests、lxml 库以及 pandas 库,结合 XPath 解析网页信息,实现对链家网二手房销售数据的爬取,并将数据导出为 Excel 文件的过程。 

            

    二、效果图

          函数功能

    • getAreasInfo(city): 该函数用于获取指定城市的各区域名称和链接信息,返回一个列表,包含区域名和链接。
    • getSinglePageInfo(city, areaname, pathname): 该函数用于获取单页的二手房销售数据,包括房屋名称、小区名、房屋信息等,返回一个 DataFrame 对象。
    • getSalesData(city): 该函数整合了前两个函数,遍历所有区域获取多页数据,并将结果保存为 Excel 文件。

            数据保存

            爬取的数据经过整理后,以 DataFrame 的形式存储,并最终通过 to_excel() 方法保存为 Excel 文件,便于后续分析和可视化展示。

    三、代码讲解

    1. import requests
    2. from bs4 import BeautifulSoup
    3. import pandas as pd

         如果出现模块报错

    c124a1693bfc457ba1f2909ee9d299fc.png

            进入控制台输入:建议使用国内镜像源

    pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple
    

             我大致罗列了以下几种国内镜像源:

    1. 清华大学
    2. https://pypi.tuna.tsinghua.edu.cn/simple
    3. 阿里云
    4. https://mirrors.aliyun.com/pypi/simple/
    5. 豆瓣
    6. https://pypi.douban.com/simple/
    7. 百度云
    8. https://mirror.baidu.com/pypi/simple/
    9. 中科大
    10. https://pypi.mirrors.ustc.edu.cn/simple/
    11. 华为云
    12. https://mirrors.huaweicloud.com/repository/pypi/simple/
    13. 腾讯云
    14. https://mirrors.cloud.tencent.com/pypi/simple/

    首先,我们导入了必要的库:

    1. import requests
    2. from lxml import etree
    3. import json
    4. import pandas as pd
    5. 接下来是一些请求所需的头信息和 cookies:
    6. cookies = {
    7. # 这里是一些 cookie 信息
    8. }
    9. headers = {
    10. # 这里是一些请求头信息
    11. }

    现在,我们定义了一个函数 getAreasInfo(city),用于获取各个区域的名称和链接:

    1. def getAreasInfo(city):
    2. # 发送请求,获取页面内容
    3. # 从页面内容中提取区域名称和链接
    4. return districts

    然后是另一个函数 getSinglePageInfo(city, areaname, pathname),用于获取单页的二手房信息:

    1. def getSinglePageInfo(city, areaname, pathname):
    2. # 发送请求,获取页面内容
    3. # 解析页面内容,提取所需的房屋信息
    4. # 将提取的信息保存到 DataFrame 中
    5. return df

    接下来是主函数 getSalesData(city),用于获取整个城市的二手房销售数据并保存到 Excel 文件:

    1. def getSalesData(city):
    2. # 获取各区域信息
    3. # 遍历各区域,调用 getSinglePageInfo() 函数获取数据
    4. # 整合数据到 DataFrame 中
    5. # 将 DataFrame 数据保存为 Excel 文件

    最后,在 if __name__ == '__main__': 中,我们调用了 getSalesData('hz') 函数以执行爬取数据的操作。

    四、完整代码:

    1. import requests
    2. from lxml import etree
    3. import re
    4. import json
    5. import pandas as pd
    6. cookies = {
    7. 'lianjia_uuid': 'd63243c2-9abd-4016-a428-7272d9bd4265',
    8. 'crosSdkDT2019DeviceId': '-5xmwrm-pv43pu-kiaob2z7e31vj11-vs7ndc7b3',
    9. 'select_city': '330100',
    10. 'digv_extends': '%7B%22utmTrackId%22%3A%22%22%7D',
    11. 'ke_uuid': 'bac7de379105ba27d257312d20f54a59',
    12. 'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%2218a8d4f86e46b6-0a2c26d29b1766-4f641677-2073600-18a8d4f86e5f7e%22%2C%22%24device_id%22%3A%2218a8d4f86e46b6-0a2c26d29b1766-4f641677-2073600-18a8d4f86e5f7e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D',
    13. 'lianjia_ssid': '6734443f-a11a-49c9-989e-8c5d2dc51185',
    14. }
    15. headers = {
    16. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    17. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    18. 'Connection': 'keep-alive',
    19. # 'Cookie': 'lianjia_uuid=d63243c2-9abd-4016-a428-7272d9bd4265; crosSdkDT2019DeviceId=-5xmwrm-pv43pu-kiaob2z7e31vj11-vs7ndc7b3; select_city=330100; digv_extends=%7B%22utmTrackId%22%3A%22%22%7D; ke_uuid=bac7de379105ba27d257312d20f54a59; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218a8d4f86e46b6-0a2c26d29b1766-4f641677-2073600-18a8d4f86e5f7e%22%2C%22%24device_id%22%3A%2218a8d4f86e46b6-0a2c26d29b1766-4f641677-2073600-18a8d4f86e5f7e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; lianjia_ssid=6734443f-a11a-49c9-989e-8c5d2dc51185',
    20. 'Referer': 'https://hz.ke.com/ershoufang/pg2/',
    21. 'Sec-Fetch-Dest': 'document',
    22. 'Sec-Fetch-Mode': 'navigate',
    23. 'Sec-Fetch-Site': 'same-origin',
    24. 'Sec-Fetch-User': '?1',
    25. 'Upgrade-Insecure-Requests': '1',
    26. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
    27. 'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Microsoft Edge";v="122"',
    28. 'sec-ch-ua-mobile': '?0',
    29. 'sec-ch-ua-platform': '"macOS"',
    30. }
    31. # 获取区的名称和路由
    32. def getAreasInfo(city):
    33. responseinit = requests.get(
    34. f'https://{city}.ke.com/ershoufang', cookies=cookies, headers=headers)
    35. html_text_init = etree.HTML(responseinit.text)
    36. districts = [z for z in zip(html_text_init.xpath('//a[@class=" CLICKDATA"]/text()'),
    37. html_text_init.xpath('//a[@class=" CLICKDATA"]/@href'))]
    38. return districts
    39. # 获取页面数据
    40. def getSinglePageInfo(city, areaname, pathname):
    41. response1 = requests.get(
    42. f'https://{city}.ke.com{pathname}pg1/', cookies=cookies, headers=headers)
    43. html_text1 = etree.HTML(response1.text)
    44. # 获取页面总数
    45. pageInfo = html_text1.xpath(
    46. '//div[@class="page-box house-lst-page-box"]/@page-data')
    47. # 数据较多,可以先设置2页,看看是否可以导出
    48. # pageTotal = json.loads(pageInfo[0])['totalPage']
    49. pageTotal = 2
    50. title = []
    51. position = []
    52. house = []
    53. follow = []
    54. totalPrice = []
    55. unitPrice = []
    56. url = []
    57. for i in range(1, pageTotal+1):
    58. response = requests.get(
    59. f'https://{city}.ke.com{pathname}pg{i}/', cookies=cookies, headers=headers)
    60. html_text = etree.HTML(response.text)
    61. ullist = html_text.xpath(
    62. '//ul[@class="sellListContent"]//li[@class="clear"]')
    63. for li in ullist:
    64. liChildren = li.getchildren()[1]
    65. # 名称
    66. title.append(liChildren.xpath('./div[@class="title"]/a/text()')[0])
    67. # url 地址
    68. url.append(liChildren.xpath('./div[@class="title"]/a/@href')[0])
    69. # 小区名称
    70. position.append(liChildren.xpath(
    71. './div/div/div[@class="positionInfo"]/a/text()')[0])
    72. # 房屋信息
    73. houselis = liChildren.xpath(
    74. './div/div[@class="houseInfo"]/text()')
    75. house.append([x.replace('\n', '').replace(' ', '')
    76. for x in houselis][1])
    77. # 上传时间
    78. followlis = liChildren.xpath(
    79. './div/div[@class="followInfo"]/text()')
    80. follow.append([x.replace('\n', '').replace(' ', '')
    81. for x in followlis][1])
    82. # 总价
    83. totalPrice.append(liChildren.xpath(
    84. './div/div[@class="priceInfo"]/div[@class="totalPrice totalPrice2"]/span/text()')[0].strip())
    85. # 单价
    86. unitPrice.append(liChildren.xpath(
    87. './div/div[@class="priceInfo"]/div[@class="unitPrice"]/span/text()')[0].replace('元/平', ""))
    88. return pd.DataFrame(dict(zip(['行政区域', '名称', '小区名', '房屋信息', '发布时间', '总价(万)', '单价(元/平)', '地址'],
    89. [areaname, title, position, house, follow, totalPrice, unitPrice, url])))
    90. def getSalesData(city):
    91. districts = getAreasInfo(city)
    92. dfInfos = pd.DataFrame()
    93. for district in districts:
    94. dfInfo = getSinglePageInfo(city, district[0], district[1])
    95. dfInfos = pd.concat([dfInfos, dfInfo], axis=0)
    96. dfInfos.to_excel(f'{city}二手房销售数据.xlsx', index=False)
    97. if __name__ == '__main__':
    98. getSalesData('hz')
    99. pass

  • 相关阅读:
    2022年度嵌入式C语言面试题库(含答案)
    深入ftrace function原理
    用CI/CD工具Vela部署Elasticsearch + C# 如何使用
    肖sir__mysql之多表练习题__007
    mysql 设置远程登录
    机器学习---CNN(创建和训练一个卷积神经网络并评估其性能)下
    【Java面试】Spring 里面的事务和分布式事务的使用如何区分,以及这两个事务之间有什么关联?
    日志(三)
    Linux学习第24天:Linux 阻塞和非阻塞 IO 实验(二): 挂起
    VoLTE基础自学系列 | VoLTE实战分析之VoLTE注册流程
  • 原文地址:https://blog.csdn.net/m0_73367097/article/details/136598540