前言

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

本次目标

爬取Q房网数据

https://shenzhen.qfang.com/newhouse

爬取目标数据:

  • 小区名字
  • 售房状态
  • 房屋面积
  • 户型
  • 开盘时间
  • 交房时间
  • 楼盘地址
  • 售价
  • 预计总价
深圳的房价到底有多高?爬取Q房网数据,有钱人真多

 

深圳的房价到底有多高?爬取Q房网数据,有钱人真多

 

emmmm,我看看就行了,买不起买不起

开发工具

  • python 3.6.5
  • pycharm
深圳的房价到底有多高?爬取Q房网数据,有钱人真多

 

爬虫代码

导入工具

import requests
import parsel
import csv

 

解析网页,爬取数据

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('rn', '').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

 

保存数据

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

运行代码,效果如下图

深圳的房价到底有多高?爬取Q房网数据,有钱人真多

 

深圳的房价到底有多高?爬取Q房网数据,有钱人真多
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/hhh188764/p/13831388.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!