自如网租房价格反爬 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()
相关推荐
Cha0DD1 小时前
【由浅入深探究langchain】第二十集-SQL Agent+Human-in-the-loop
人工智能·python·ai·langchain
Cha0DD1 小时前
【由浅入深探究langchain】第十九集-官方的SQL Agent示例
人工智能·python·ai·langchain
thatway19891 小时前
闲聊-关于AI终结者的警醒
前端
努力的小郑1 小时前
突发!Claude Code 51万行源码全网裸奔:一场史诗级“开源”事故,国内大厂笑麻了
前端·后端·ai编程
七度黑光2 小时前
用 openclaw 给故障复盘打分:质量审核自动化实践
运维·服务器·前端·数据库·自动化
HashTang2 小时前
Claude Code 源码中 REPL.tsx 深度解析:一个 5005 行 React 组件的架构启示
前端·后端·ai编程
wendycwb3 小时前
前端城市地址根据最后一级倒推,获取各层级id的方法
前端·vue.js·typescript
智算菩萨3 小时前
【Tkinter】4 Tkinter Entry 输入框控件深度解析:数据验证、密码输入与现代表单设计实战
python·ui·tkinter·数据验证·entry·输入框
终端鹿3 小时前
Vue3 模板引用 (ref):操作 DOM 与子组件实例 从入门到精通
前端·javascript·vue.js
千寻girling3 小时前
不知道 Java 全栈 + AI 编程有没有搞头 ?
前端·人工智能·后端