You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用requests_html爬取script标签仅返回type属性,如何获取内部JSON数据

提取application/ld+json标签内目标字段的解决方法

你当前输出仅返回标签属性,是因为r.html.find()返回的是requests-html的Element对象,需要按以下流程获取目标数据:

  • 第一步:精准定位目标script标签
    避免使用索引下标定位(页面结构变化会导致索引失效),直接通过type属性筛选对应标签:
    target_script = r.html.find('script[type="application/ld+json"]', first=True)
    
  • 第二步:提取标签内部的JSON文本
    调用Element对象的text属性获取标签包裹的纯JSON字符串:
    json_content = target_script.text
    
  • 第三步:解析JSON并提取目标字段
    导入Python内置json模块将字符串解析为字典,之后直接取值即可:
    import json
    
    data = json.loads(json_content)
    # 提取品牌名称
    brand_name = data["brand"]["name"]
    # 提取商品描述
    product_desc = data["description"]
    

内容的提问来源于stack exchange,提问作者lanavargas2002

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 21:06:03