ihgougoguoig 2021-10-30 12:56 采纳率: 50%
浏览 185
已结题

JSONDecodeError: Expecting value: line 1 column 1 (char 0)



import requests,json
response_json =requests.get('https://weibo.com/ajax/profile/info?uid=1810032213').text
json.loads(response_json)




JSONDecodeError                           Traceback (most recent call last)
<ipython-input-1-8966098e9114> in <module>
      1 import requests,json
      2 response_json =requests.get('https://weibo.com/ajax/profile/info?uid=1810032213').text
----> 3 json.loads(response_json)

D:\Ananaconda\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

D:\Ananaconda\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

D:\Ananaconda\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)


——————————————————
这里request到的,text:
<!DOCTYPE html>\n<html>\n<head>\n    <meta http-equiv="Content-type" content="text/html; charset=gb2312"/>\n    <title>Sina Visitor System</title>\n</head>\n<body>\n<span id="message"></span>\n<script type="text/javascript" src="/js/visitor/mini_original.js?v=20161116"></script>\n<script type="text/javascript">\n    window.use_fp = "1" == "1"; // ÊÇ·ñ²É¼¯É豸ָÎÆ¡£\n    var url = url || {};\n    (function () {\n        this.l = function (u, c) {\n            try {\n                var s = document.createElement("script");\n                s.type = "text/javascript";\n                s[document.all ? "onreadystatechange" : "onload"] = function () {\n\n                    if (document.all && this.readyState != "loaded" && this.readyState != "complete") {\n                        return\n                    }\n                    this[document.all ? "onreadystatechange" : "onload"] = null;\n                    this.parentNode.removeChild(this);\n                    if (c) {\n                        c()\n                    }\n                };\n                s.src = u;\n                document.getElementsByTagName("head")[0].appendChild(s)\n            } catch (e) {\n            }\n        };\n    }).call(url);\n\n    // Á÷³ÌÈë¿Ú¡£\n    wload(function () {\n\n        try {\n\n            var need_restore = "1" == "1"; // ÊÇ·ñ×߻ָ´Éí·ÝÁ÷³Ì¡£\n\n            // Èç¹ûÐèÒª×߻ָ´Éí·ÝÁ÷³Ì£¬³¢ÊÔ´Ó cookie »ñÈ¡Óû§Éí·Ý¡£\n            if (!need_restore || !Store.CookieHelper.get("SRF")) {\n\n                // Èô»ñȡʧ°Ü×ß´´½¨·Ã¿ÍÁ÷³Ì¡£\n                // Á÷³ÌÖ´ÐÐʱ¼ä¹ý³¤£¨³¬¹ý 3s£©£¬ÔòÈÏΪ³ö´í¡£\n                var error_timeout = window.setTimeout("error_back()", 5000);\n\n                tid.get(function (tid, where, confidence) {\n                    // È¡Ö¸ÎÆ˳ÀûÍê³É£¬Çå³ý³ö´í timeout ¡£\n                    window.clearTimeout(error_timeout);\n                    incarnate(tid, where, confidence);\n                });\n            } else {\n                // Óû§Éí·Ý´æÔÚ£¬³¢ÊÔ»Ö¸´Óû§Éí·Ý¡£\n                restore();\n            }\n        } catch (e) {\n            // ³ö´í¡£\n            error_back();\n        }\n    });\n\n    // ¡°·µ»Ø¡± »Øµ÷º¯Êý¡£\n    var return_back = function (response) {\n\n        if (response["retcode"] == 20000000) {\n            back();\n        } else {\n            // ³ö´í¡£\n            error_back(response["msg"]);\n        }\n    };\n\n    // Ìøת»Ø³õʼµØÖ·¡£\n    var back = function() {\n\n        var url = "https://weibo.com/ajax/profile/info?uid=1810032213";\n        if (url != "none") {\n            window.location.href = url;\n        }\n    };\n\n    // ¿çÓò¹ã²¥¡£\n    var cross_domain = function (response) {\n\n        var from = "weibo";\n        var entry = "sinawap";\n        if (response["retcode"] == 20000000) {\n\n            var crossdomain_host = "login.sina.com.cn";\n            if (crossdomain_host != "none") {\n\n                var cross_domain_intr = window.location.protocol + "//" + crossdomain_host + "/visitor/visitor?a=crossdomain&cb=return_back&s=" +\n                        encodeURIComponent(response["data"]["sub"]) + "&sp=" + encodeURIComponent(response["data"]["subp"]) + "&from=" + from + "&_rand=" + Math.random() + "&entry=" + entry;\n                url.l(cross_domain_intr);\n            } else {\n\n                back();\n            }\n        } else {\n\n            // ³ö´í¡£\n            error_back(response["msg"]);\n        }\n    };\n\n    // ΪÓû§¸³Óè·Ã¿ÍÉí·Ý ¡£\n    var incarnate = function (tid, where, conficence) {\n\n        var gen_conf = "";\n        var from = "weibo";\n        var incarnate_intr = window.location.protocol + "//" + window.location.host + "/visitor/visitor?a=incarnate&t=" +\n                encodeURIComponent(tid) + "&w=" + encodeURIComponent(where) + "&c=" + encodeURIComponent(conficence) +\n                "&gc=" + encodeURIComponent(gen_conf) + "&cb=cross_domain&from=" + from + "&_rand=" + Math.random();\n        url.l(incarnate_intr);\n    };\n\n    // »Ö¸´Óû§¶ªÊ§µÄÉí·Ý¡£\n    var restore = function () {\n\n        var from = "weibo";\n        var restore_intr = window.location.protocol + "//" + window.location.host +\n                "/visitor/visitor?a=restore&cb=restore_back&from=" + from + "&_rand=" + Math.random();\n\n        url.l(restore_intr);\n    };\n\n    // ¿çÓò»Ö¸´¶ªÊ§µÄÉí·Ý¡£\n    var restore_back = function (response) {\n\n        // Éí·Ý»Ö¸´³É¹¦×߹㲥Á÷³Ì£¬·ñÔò×ß´´½¨·Ã¿ÍÁ÷³Ì¡£\n        if (response["retcode"] == 20000000) {\n\n            var url = "https://weibo.com/ajax/profile/info?uid=1810032213";\n            var alt = response["data"]["alt"];\n            var savestate = response["data"]["savestate"];\n            if (alt != "") {\n                requrl = (url == "none") ? "" : "&url=" + encodeURIComponent(url);\n                var params = "entry=sso&alt=" + encodeURIComponent(alt) + "&returntype=META" +\n                    "&gateway=1&savestate=" + encodeURIComponent(savestate) + requrl;\n                window.location.href = "https://login.sina.com.cn/sso/login.php?" + params;\n            } else {\n\n                cross_domain(response);\n            }\n        } else if(response[\'retcode\'] == 50111261 && isInIframe()) {\n            //do nothing\n        } else {\n\n            tid.get(function (tid, where, confidence) {\n                incarnate(tid, where, confidence);\n            });\n        }\n    };\n\n    // ³ö´íÇé¿ö·µ»ØµÇ¼ҳ¡£\n    var error_back = function (msg) {\n\n        var url = "https://weibo.com/ajax/profile/info?uid=1810032213";\n        var clientType = "mobile";\n        if (url != "none") {\n\n            if (url.indexOf("ssovie4c55=0") === -1) {\n                url += (((url.indexOf("?") === -1) ? "?" : "&") + "ssovie4c55=0");\n            }\n            if (clientType == "mobile") {\n            \twindow.location.href = "https://passport.weibo.cn/signin/login?r="+url;\n            } else{\n            \twindow.location.href = "https://weibo.com/login.php";\n            }\n        } else {\n\n            if(document.getElementById("message")) {\n                document.getElementById("message").innerHTML = "Error occurred" + (msg ? (": " + msg) : ".");\n            }\n        }\n    };\n\n    var isInIframe = function () {\n        try {\n            return window.self !== window.top;\n        } catch (e) {\n            return true;\n        }\n    };\n\n</script>\n</body>\n</html>'

但是用网页打开的时候


{"ok":1,"data":{"user":{"id":1810032213,"idstr":"1810032213","pc_new":7,"screen_name":"荆雪莉rA24","profile_image_url":"https://tvax1.sinaimg.cn/crop.0.0.1002.1002.50/6be2e655ly8gb2r8itwezj20ru0ruq47.jpg?KID=imgbed,tva&Expires=1635580428&ssig=Ruqs%2B1ewC8","profile_url":"/u/1810032213","verified":false,"verified_type":-1,"domain":"","weihao":"","avatar_large":"https://tvax1.sinaimg.cn/crop.0.0.1002.1002.180/6be2e655ly8gb2r8itwezj20ru0ruq47.jpg?KID=imgbed,tva&Expires=1635580428&ssig=iSAW%2BSuFMM","avatar_hd":"https://tvax1.sinaimg.cn/crop.0.0.1002.1002.1024/6be2e655ly8gb2r8itwezj20ru0ruq47.jpg?KID=imgbed,tva&Expires=1635580428&ssig=gRQQp1M62p","follow_me":false,"following":false,"mbrank":0,"mbtype":0,"planet_video":true,"description":"","location":"四川 自贡","gender":"m","followers_count":22,"followers_count_str":"22","friends_count":513,"statuses_count":325,"url":"http://shenyuetian1992.blog.ifeng.com/","cover_image_phone":"http://ww1.sinaimg.cn/crop.0.0.640.640.640/549d0121tw1egm1kjly3jj20hs0hsq4f.jpg","user_type":2,"is_muteuser":false,"special_follow":false},"tabList":[{"name":"home","tabName":"精选"},{"name":"feed","tabName":"微博"},{"name":"album","tabName":"相册"}]}}

请问这是为什么啊

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-10-30 13:13
    关注

    requests要附带上cookie,第一次访问没有cookie,访问这个网址会跳转到登录页面并生成一些验证的cookie,然后再跳转回原来的网址,所以没附带cookie,得到的并不是最后的json内容。而且最好加上user-agent,要不可能反扒会检查这个

    下面的这个就正常了

    img

    import requests,json
    headers={
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
        'cookie':'SINAGLOBAL=2002793067242.412.1621560743299; ALF=1664542397; SCF=AmX3HjMJH4xXPUgn6XTsnfaokCAXDWkwlVCBEzdyfnbQS_mSGmY2_G63tjyJkjCnADmQv41qaoTaT1srDq06GIY.; UOR=login.sina.com.cn,service.weibo.com,britishmuseum.org.cn; ULV=1634789521215:7:1:1:9968473885022.389.1634789521119:1633006433154; XSRF-TOKEN=btSMV0MxYaLrGHfXhnjloqs7; SUB=_2AkMWIFwJf8NxqwJRmP8dyGnjaIp0zQDEieKgfK3SJRMxHRl-yT9jqmpatRB6PaBy5mtJLQ1-qlkd_ijVAb2CRJW7YA17; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWbGTTZ9zIuyqIW0eW6n2Xd; WBPSESS=5fStQf4aE0d6e7rh9d-P6gjyDeDWGix3bO5fMGSMIgHZa-N0S3u7j_n3Y1iiOwe8jN6qaGw2sokNgIkgDoJX6QQYRoVwgwtYtfxsklSTeEGQtAjPewLY4STnH446c5EHpbWVEvG7DD0L6xbNp9UJmVuF_Feh7L5OQi65TRDOiNw='
    }
    response_json =requests.get('https://weibo.com/ajax/profile/info?uid=1810032213',headers=headers).text
    o=json.loads(response_json)
    print(o)
    
    

    cookie获取:F12打开开发工具,看网络选项卡送的请求,如果没有刷新过一次页面就会出现了

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月23日
  • 已采纳回答 11月15日
  • 创建了问题 10月30日

悬赏问题

  • ¥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驱动开发环境