我正在尝试运行以下代码:
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。
谢谢
答案 0 :(得分:0)
发布后你的例子是不完整的,所以很难肯定,但不管你是否确定它在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字符串)。
答案 1 :(得分:0)
行 标题 是它应该存在的问题
"{
":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"
}"