爬取贝壳租房的数据

爬取贝壳租房的数据

from csv import writer
from re import fullmatch

import os
import requests
from bs4 import BeautifulSoup


def get_url(start_page=1, end_page=2):
    # 如果'file'文件不存在就创建
    if not os.path.exists(r'file'):
        os.mkdir(r'file')

    # 打开'file'下的'house.csv'文件
    f = open(r'file/house.csv', 'w', encoding='utf-8', newline='')
    w1 = writer(f)
    # 存入第一行
    w1.writerow(['名字', '地址', '面积', '样式', '价格'])

    # 设置起始页和终止页并遍历
    for page in range(start_page, end_page):

        url = rf'https://cd.zu.ke.com/zufang/pg{page}/#contentList'

        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
        }

        response = requests.get(url, headers=headers)
        result = response.text

        # 创建soup对象
        soup = BeautifulSoup(result, 'lxml')
        
        # 数据筛选
        div_house = soup.select('.content__article>div>div')
        
        for x in div_house:
            # 提取名字
            name = x.select_one('.twoline').text.strip()
            
            # 提取p标签里的内容
            msg = x.select('div>p')
            
            # 对提取到的内容进行处理,获取地址add,面积area,样式pattern
            msg_list = [y.text.strip().split('\n') for y in msg]
            if fullmatch(r'\w+\s+/', msg_list[1][0]):
                add = msg_list[1][1]
                area = msg_list[1][3].strip()
                pattern = msg_list[1][5].strip()
            elif fullmatch(r'\w+-\w+-\w+', msg_list[1][0]):
                add = msg_list[1][0]
                area = msg_list[1][2].strip()
                pattern = msg_list[1][4].strip()
            else:
                continue
                
            # 获取价格
            price = x.select_one('.content__list--item-price').text
            
            # 将数据写入csv文件
            w1.writerow([name, add, area, pattern, price])
            
        print(f'第{page}页加载完成')
        
    # 关闭文件
    f.close()


if __name__ == '__main__':
    get_url(1, 101)
爬取贝壳找房网站的二手房和租房数据,获取每个房源的标题位置和价格,通常需要通过编写网络虫程序来实现。这一过程主要包括以下步骤: 1. 分析网页结构:首先,需要使用浏览器的开发者工具来检查贝壳找房网站的房源列表页面,了解房源信息是如何在HTML中组织的。通常情况下,房源标题和价格会以特定的标签、类名或ID存在于页面的HTML代码中。 2. 编写虫代码:根据分析结果,可以使用Python中的虫框架如Scrapy或者通用的HTTP请求库如requests来发送网络请求,获取网页内容。然后使用BeautifulSoup、lxml等库解析HTML,提取出房源的标题和价格信息。 3. 存储数据:提取出的数据需要被存储起来,可以保存为CSV、JSON或者直接存入数据库中,以便后续的数据分析或使用。 这里是一个简化的代码示例,展示如何使用Python和BeautifulSoup来提取网页中的房源标题和价格信息: ```python import requests from bs4 import BeautifulSoup # 发送GET请求获取网页内容 url = '目标房源列表页面URL' response = requests.get(url) response.encoding = response.apparent_encoding # 确保正确的编码 # 解析HTML内容 soup = BeautifulSoup(response.text, 'html.parser') # 查找包含房源信息的标签,这里假设是class为"house-info"的div标签 houses = soup.find_all('div', class_='house-info') # 遍历房源标签,提取标题和价格 for house in houses: title = house.find('a', class_='title').text.strip() # 假设标题在class为"title"的a标签中 price = house.find('span', class_='price').text.strip() # 假设价格在class为"price"的span标签中 print(f'房源标题:{title}, 价格:{price}') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值