Python爬取Q房网全新楼盘房价信息,有钱人真的多!

143 阅读2分钟

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

python免费学习资料以及群交流解答点击即可加入

开发工具

  • python 3.6.5

  • pycharm

    import requests import parsel import csv 123

相关模块 pip 安装即可

确定目标网页

![](https://p1-tt-ipv6.byteimg.com/large/pgc-image/2dd40eba31694e71aed8c7788af10cac)

通过开发者工具可以发现,该网页为静态网页,对于数据的抓取还是比较简单的~

爬取目标数据:

  • 小区名字
  • 售房状态
  • 房屋面积
  • 户型
  • 开盘时间
  • 交房时间
  • 楼盘地址
  • 售价
  • 预计总价
![](https://p6-tt-ipv6.byteimg.com/large/pgc-image/823ad9d4de0f4fc187494116e563ec24)

网站比较简单就不一一分析了~ 直接上代码

import requests
import parsel
import csv
f = open('房产数据.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['标题', '房产状态', '售房面积', '户型', '开盘时间', '出售时间', '地址', '售价', '预计总价'])
csv_writer.writeheader()

for page in range(1, 84):
    print('===============================正在爬取第{}页的数据================================================='.format(page))
    url = 'https://shenzhen.qfang.com/newhouse/list/n{}'.format(page)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    selector = parsel.Selector(response.text)
    lis = selector.css('.list-result li')
    dit = {}
    for li in lis:
        title = li.css('.list-main-header a em::text').get()  # 名字
        dit['标题'] = title
        status = li.css('.list-main-header i::text').get()     # 是否在售
        dit['房产状态'] = status
        space = li.css('.list-main div:nth-child(1) .space span::text').get()     # 售房面积
        dit['售房面积'] = space
        type_list = li.css('.list-main.fl p:nth-child(3) span a::text').getall()     # 户型
        type_str = '|'.join(type_list).strip().replace('\r\n', '').replace(' ', '')  # 户型
        dit['户型'] = type_str
        kp_time = li.css('.new-house-info > div:nth-child(2) > p.space.fl.clearfix > span::text').get()  # 开盘时间
        dit['开盘时间'] = kp_time
        cs_time = li.css('.new-house-info > div:nth-child(2) > p:nth-child(3)> span::text').get()  # 出售时间
        dit['出售时间'] = cs_time
        address = li.css('.list-main a:nth-child(3)::text').get()  # 地址

        if not address == None:
            address = address.strip()
        else:
            address = None
        dit['地址'] = address
        Price = li.css('.list-price .bigger .amount::text').get()  # 售价
        dit['售价'] = Price
        hj_Price = li.css('.list-price .smaller::text').get()   # 预计总价
        dit['预计总价'] = hj_Price
        csv_writer.writerow(dit)
        print(dit)
1234567891011121314151617181920212223242526272829303132333435363738394041424344

运行实现效果

![](https://p26-tt.byteimg.com/large/pgc-image/9f2a926ce02e42d98dcf127d2d81052d)
![](https://p1-tt-ipv6.byteimg.com/large/pgc-image/6331fe182a5e42a784f242a4d08f8ec0)

一共是83页数据1648个楼盘信息,有大概600个左右的楼盘已经售罄了

有钱人还是真的多啊~