定向思维重度患者 2022-09-25 17:13 采纳率: 72.7%
浏览 40
已结题

刚从网页上爬下来的这个标签,有办法可以将其中的数据改成字典类型吗?


<script>
    var AppData = {"name":"bangdan","query":{"year":"0","term":"0","id":"39","timeStamp":"1661645982475","channelId":"40011","index":"3","signKey":"b8833acba83e765770c80d6fc06d6371","sVersion":"1","webdriver":"false"},"$showAppLink":true,"$schema":"meituanmovie://www.meituan.com/web?url=https%3A%2F%2Fi.maoyan.com%2Fasgard%2Fboard%3Fid%3D39%26year%3D0%26term%3D0","$urlQuery":{"year":"0","term":"0","limit":10,"offset":0},"browser":{"ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.63","name":"chrome","version":"104.0.5112","vender":"apple","platform":"unknow","platformVersion":"unknow","core":{"name":"chrome","version":104},"type":"normal"},"$browser":{"ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.63","name":"chrome","version":"104.0.5112","vender":"apple","platform":"unknow","platformVersion":"unknow","core":{"name":"chrome","version":104},"type":"normal"},"$title":"猫眼榜单","$description":"猫眼榜单,猫眼Top100","$keywords":"猫眼榜单,猫眼Top100","$url":"https://i.maoyan.com/asgard/board?year=0&term=0&id=39&timeStamp=1661645982475&channelId=40011&index=3&signKey=b8833………………

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-09-25 19:28
    关注

    用正则或者字符串前后截取下,用json.loads加载生成字典

    完整的html是什么,现在有开始字符,需要找到结束字符是什么才好截取。
    比如下面这样,题主改下开始和结束的内容

    import json
    
    html=''' 
    <script>
        var AppData = {"name":"bangdan","query":{"year":"0","term":"0","id":"39","timeStamp":"1661645982475","channelId":"40011","index":"3","signKey":"b8833acba83e765770c80d6fc06d6371","sVersion":"1","webdriver":"false"},"$showAppLink":true,"$schema":"meituanmovie://www.meituan.com/web?url=https%3A%2F%2Fi.maoyan.com%2Fasgard%2Fboard%3Fid%3D39%26year%3D0%26term%3D0","$urlQuery":{"year":"0","term":"0","limit":10,"offset":0},"browser":{"ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.63","name":"chrome","version":"104.0.5112","vender":"apple","platform":"unknow","platformVersion":"unknow","core":{"name":"chrome","version":104},"type":"normal"},"$browser":{"ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.63","name":"chrome","version":"104.0.5112","vender":"apple","platform":"unknow","platformVersion":"unknow","core":{"name":"chrome","version":104},"type":"normal"},"$title":"猫眼榜单","$description":"猫眼榜单,猫眼Top100","$keywords":"猫眼榜单,猫眼Top100","$url":"https://i.maoyan.com/asgard/board?year=0&term=0&id=39&timeStamp=1661645982475&channelId=40011&index=3&signKey=b8833………………"}'''
    
    sstart="var AppData = "#开始字符串
    send='………"}'#结束字符串,这里如果不是json内容一部分,下面的截取代码不要加上结束字符串
    startindex=html.find(sstart)+len(sstart)#开始位置
    endindex=html.find(send,startindex)#结束位置
    
    s=html[startindex:endindex]+send
    o=json.loads(s)
    
    print(o)
    print(o['$url'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 9月25日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境