python爬虫——使用bs4爬取链家网的房源信息

1. 先看效果

在这里插入图片描述
在这里插入图片描述

2. 进入链家网,这里我选择的是海口市点击跳转到链家网

在这里插入图片描述

3. 先看网页的结构,这些房子的信息都在li标签,而li标签再ul标签,所以怎么做大家都懂

在这里插入图片描述

4. 代码如下,url的链接大家可以自己翻页看看就找到规律了,这里就不多说了

url = 'https://hk.lianjia.com/ershoufang/pg{}/'.format(page)
# 请求url
resp = requests.get(url, headers=headers)
# 讲返回体转换成Beautiful
soup = BeautifulSoup(resp.content, 'lxml')
# 筛选全部的li标签
sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')

5. 再看看我们需要提取信息的结构

在这里插入图片描述

6. 代码附上,其中会做一些数据清洗,大家爬的时候就知道了,对了,其中stripped_strings返回来的是一个迭代器,所以需要转换成list

# 循环遍历
    for sell in sellListContent:
        try:
            # 题目
            title = sell.select('div.title a')[0].string
            # 先抓取全部的div信息,再针对每一条进行提取
            houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)
            # 楼盘名字
            loupan = houseInfo[0]
            # 对剩下的信息进行分割
            info = houseInfo[1].split('|')
            # 房子类型
            house_type = info[1].strip()
            # 面积
            area = info[2].strip()
            # 朝向
            toward = info[3].strip()
            # 装修类型
            renovation = info[4].strip()
            # 地址
            positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))
            # 总价
            totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))
            # 单价
            unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]

            # 声明一个字典存储数据
            data_dict = {}
            data_dict['title'] = title
            data_dict['loupan'] = loupan
            data_dict['house_type'] = house_type
            data_dict['area'] = area
            data_dict['toward'] = toward
            data_dict['renovation'] = renovation
            data_dict['positionInfo'] = positionInfo
            data_dict['totalPrice'] = totalPrice
            data_dict['unitPrice'] = unitPrice

            data_list.append(data_dict)
            print(data_dict)

7. 完整代码附上

import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
import json
import csv
import time


# 构建请求头
ua = UserAgent()
headers = {
    'user-agent': ua.Chrome
}

# 声明一个列表存储字典
data_list = []


def start_spider(page):
    url = 'https://hk.lianjia.com/ershoufang/pg{}/'.format(page)
    # 请求url
    resp = requests.get(url, headers=headers)
    # 讲返回体转换成Beautiful
    soup = BeautifulSoup(resp.content, 'lxml')
    # 筛选全部的li标签
    sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')
    # 循环遍历
    for sell in sellListContent:
        try:
            # 题目
            title = sell.select('div.title a')[0].string
            # 先抓取全部的div信息,再针对每一条进行提取
            houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)
            # 楼盘名字
            loupan = houseInfo[0]
            # 对剩下的信息进行分割
            info = houseInfo[1].split('|')
            # 房子类型
            house_type = info[1].strip()
            # 面积
            area = info[2].strip()
            # 朝向
            toward = info[3].strip()
            # 装修类型
            renovation = info[4].strip()
            # 地址
            positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))
            # 总价
            totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))
            # 单价
            unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]

            # 声明一个字典存储数据
            data_dict = {}
            data_dict['title'] = title
            data_dict['loupan'] = loupan
            data_dict['house_type'] = house_type
            data_dict['area'] = area
            data_dict['toward'] = toward
            data_dict['renovation'] = renovation
            data_dict['positionInfo'] = positionInfo
            data_dict['totalPrice'] = totalPrice
            data_dict['unitPrice'] = unitPrice

            data_list.append(data_dict)
            print(data_dict)
        except Exception as e:
            continue


def main():

    # 只爬取10页
    for page in range(1, 10):
        start_spider(page)
        time.sleep(3)

    # 将数据写入json文件
    with open('data_json.json', 'a+', encoding='utf-8') as f:
        json.dump(data_list, f, ensure_ascii=False, indent=4)
    print('json文件写入完成')

    # 将数据写入csv文件
    with open('data_csv.csv', 'w', encoding='utf-8', newline='') as f:
        # 表头
        title = data_list[0].keys()
        # 创建writer对象
        writer = csv.DictWriter(f, title)
        # 写入表头
        writer.writeheader()
        # 批量写入数据
        writer.writerows(data_list)
    print('csv文件写入完成')


if __name__ == '__main__':

    main()
  • 15
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值