自如网租房价格反爬 CSS偏移

python 复制代码
import re
import requests
from io import BytesIO
import ddddocr
from lxml import etree


def get_yellow_price(img_url):
    response = requests.get(img_url).content
    img_bytes = BytesIO(response)
    ocr = ddddocr.DdddOcr(beta=True, show_ad=False)
    yellow_price = ocr.classification(img_bytes.getvalue())
    return yellow_price
        

def main():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
    }
    response = requests.get('https://www.ziroom.com/z/', headers=headers).text
    
    # 黄色价格图片
    img_url = 'https:' + re.search('//static8.ziroom.com/phoenix/pc/images/price/new-list/(.*?).png', response).group()
    yellow_price = get_yellow_price(img_url)

    html = etree.HTML(response)
    div_list = html.xpath('//div[@class="Z_list-box"]/div') # 第5个div是广告
    del div_list[4]  # 删除索引为4的元素(第5个元素)  
    for div in div_list:
        title = div.xpath('./div[3]/h5/a/text()')[0]
        styles = div.xpath('.//div[3]/div[2]/div/span[position()>1]/@style')
        price = ''
        for style in styles:
            pos = re.findall(r'background-position: -(.*?)px', style)[0]
            # 红色字体价格 固定
            if 'red.png' in style:
                red_price = '8652039147'  # 红色价格数字  不变
                pos_price = red_price[int(float(pos)/20)]
                
            # 黄色字体价格 
            else:
                pos_price = yellow_price[int(float(pos)/21.4)]
            price += pos_price

        print(f"{title} ¥{price}/月")


if __name__ == "__main__":
    main()
相关推荐
后台开发者Ethan4 分钟前
setState组件更新
前端·javascript·react
汤米粥4 分钟前
Python爬虫中Xpath用法之——normalize-space()
开发语言·python
谙忆10249 分钟前
大文件上传实战:前端分片、断点续传、秒传与并发控制怎么落地
前端
想会飞的蒲公英24 分钟前
回归模型怎样评估:MAE、MSE、RMSE 和 R²
人工智能·python·机器学习·数据分析·回归
henrylin999932 分钟前
Quant Agent 完整系统规格
python·量化
三川69834 分钟前
Tkinter库的学习记录10-Message与Messagebox
python
吃饱了得干活34 分钟前
别只会传 temperature!一文挖透 LangChain 模型配置:Profile、全参数与运行时动态覆盖
python·langchain
轻动琴弦36 分钟前
VoltTool —— 一站式在线开发者工具平台,69+实用工具免费使用
前端·chrome
就叫飞六吧38 分钟前
不同站点iframe嵌套种cookie、localstorege.set() 关系
前端