【问题标题】:Python code runs without problems in Py2.7 but doesn't in 3.6.4Python 代码在 Py2.7 中运行没有问题,但在 3.6.4 中没有
【发布时间】:2018-08-17 11:16:02
【问题描述】:

我正在尝试运行以下代码:

Headers=requests.get('https://coinmarketcap.com').headers
CookieDough=Headers['Set-Cookie']
Headers="""\"{
":authority":"coinmarketcap.com",
":method":"POST",
":path":"/login",
":scheme":"https",
"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding":"gzip, deflate, br",
"accept-language":"en-US,en;q=0.9",
"cache-control":"max-age=0",
"content-length":"743",
"content-type":"application/x-www-form-urlencoded",
"cookie":\""""+CookieDough+"""\",
"origin":"https://coinmarketcap.com",
"referer":"https://coinmarketcap.com",
"upgrade-insecure-requests":"1",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
}\""""

print(json.loads(Headers, strict=False))

在过去的一个线程中,有人在 python 2.7 上运行了此代码并且它运行成功,但是当我在我的最后运行它时,python 3.6.4,它没有运行。它给了我以下错误:

json.decoder.JSONDecodeError: Extra data: line 2 column 2 (char 4)

我在 Sublime Text 3 Builder 上运行 Python 3.6.4。

谢谢

【问题讨论】:

    标签: python python-3.x python-2.7 sublimetext3


    【解决方案1】:

    正如发布的那样,您的示例不完整,因此很难确定,但无论如何,您确定它在 Python 2.7 中有效,因为未经修改,它不适用于我在 Python 2.7 或 3.6 版本中。

    具体来说,这是一个问题:

    Headers="""\"{
    }\""""
    

    字符串中的第一个和最后一个字符是\",这是一个常规的双引号字符。这意味着Headers 的值如下所示:

    "{
    ":authority":"coinmarketcap.com",
    ":method":"POST",
    ":path":"/login",
    ":scheme":"https",
    "accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding":"gzip, deflate, br",
    "accept-language":"en-US,en;q=0.9",
    "cache-control":"max-age=0",
    "content-length":"743",
    "content-type":"application/x-www-form-urlencoded",
    "cookie":"stuff",
    "origin":"https://coinmarketcap.com",
    "referer":"https://coinmarketcap.com",
    "upgrade-insecure-requests":"1",
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }"
    

    这不是有效的 JSON;它是一个嵌入了换行符的字符串。可能您想从开头和结尾删除有问题的\"

    如果打算让它看起来像包含字典的字符串,那么根据定义,它已经是这样了,您不需要使用json 模块将其解码为字符串。

    如果打算让它成为字典,则删除这些字符使其再次有效(假设 CookieDough 中的任何内容都是有效的 JSON 字符串)。

    【讨论】:

      【解决方案2】:

      Line Headers 应该是问题 "{ ":authority":"coinmarketcap.com", ":method":"POST", ":path":"/login", ":scheme":"https", "accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "accept-encoding":"gzip, deflate, br", "accept-language":"en-US,en;q=0.9", "cache-control":"max-age=0", "content-length":"743", "content-type":"application/x-www-form-urlencoded", "cookie":"stuff", "origin":"https://coinmarketcap.com", "referer":"https://coinmarketcap.com", "upgrade-insecure-requests":"1", "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" }"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 2022-12-25
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多