爬虫——租房——暴力法

对平台上发布的租房信息进行抓取

分析网页

在这里插入图片描述
(一)这个网站的画相对简单一点,每条信息基本都是存放在一个 li 标签下(二)选择用 BeautifulSoup 进行解析
(三)再根据属性值进行定位
(四)用 get_text() 获取标签文本
这里我就直接数了一下获取到的每一个值,直接列表切割得到数据
在这里插入图片描述

代码

简单易操作,笨人有笨方法,直接上,注释都懒了

# 心机小兔的python之路
# @Time : 2022/5/20 11:46
import requests
from bs4 import BeautifulSoup

def get_html(url,headers):
    html = requests.get(url,headers=headers)
    soup = BeautifulSoup(html.text,'lxml')
    li_list = soup.find_all('li')
    for li in li_list:
        try:
            directory = li.find('a',class_="strongbox").get_text()[25:55]
            size1 = li.find('p',class_="room").get_text()[0:2]
            size2 = li.find('p',class_="room").get_text()[26:29]
            locations = li.find('p',class_="infor").get_text().split()
            location = locations[0] + "," + locations[1] + "," + locations[2]
            price = li.find('div',class_="money").get_text()[1:9]
            item = {}
            item['directory'] = directory
            item['size1'] = size1
            item['size2'] = size2
            item['location'] = location
            item['price'] = price
            print(item)
        except:
            pass



if __name__ == '__main__':
    url = 'https://gz.58.com/zufang/'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32'
    }
    get_html(url,headers=headers)

总结

其实方法多的是,只是刚好回顾了一下bs4,心血来潮敲两下,也没写文件的保存,有兴趣的兄弟可以试着往下写,不难

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Python 爬虫实现,用于从北京租房信息网站上爬取租房信息并将其导入到 Excel 文件中。 首先,我们需要安装以下 Python 库: - requests:用于发送 HTTP 请求和获取网页内容。 - Beautiful Soup:用于解析 HTML 和 XML 网页内容。 - openpyxl:用于操作 Excel 文件。 可以使用 pip 命令进行安装: ``` pip install requests beautifulsoup4 openpyxl ``` 接下来,我们需要编写 Python 代码: ```python import requests from bs4 import BeautifulSoup from openpyxl import Workbook # 发送 HTTP 请求并获取网页内容 url = 'https://bj.zu.anjuke.com/fangyuan/p1/' response = requests.get(url) html = response.text # 使用 Beautiful Soup 解析网页内容 soup = BeautifulSoup(html, 'html.parser') houses = soup.select('.zu-itemmod') # 创建 Excel 文件并添加表头 wb = Workbook() ws = wb.active ws.append(['标题', '链接', '小区', '面积', '租金']) # 遍历租房信息并将其添加到 Excel 文件中 for house in houses: title = house.select('.zu-info h3 a')[0].text.strip() link = house.select('.zu-info h3 a')[0]['href'] community = house.select('.details-item')[0].text.strip() area = house.select('.details-item')[1].text.strip() price = house.select('.zu-side strong')[0].text.strip() ws.append([title, link, community, area, price]) # 保存 Excel 文件 wb.save('beijing_rent.xlsx') ``` 该爬虫程序将会从北京租房信息网站的第一页开始爬取租房信息,包括标题、链接、小区、面积和租金,并将其添加到 Excel 文件中。你可以根据需要修改代码以实现更多功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心机小兔

你的鼓励会让我更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值