美文网首页Python
[Python]requests 得到headers解析为dic

[Python]requests 得到headers解析为dic

作者: _小老虎_ | 来源:发表于2018-10-11 22:10 被阅读310次

    我们看看headers的type,是CaseInsensitiveDict类.

    r = requests.get("http:...")
    In [99]: type(r.headers)
    Out[99]: requests.structures.CaseInsensitiveDict
    

    不能直接拿来用

    • 解决方法
    In [96]: json.dumps(dict(r.headers))
    Out[96]: '{"x-powered-by": "PHP/5.3.3", "transfer-encoding": "chunked", "content-encoding": "gzip", "server": "nginx/1.8.0", "connection": "keep-alive", "date": "Mon, 25 Jan 2016 09:56:54 GMT", "content-type": "text/html; charset=UTF-8", "x-pingback": "http://xiaorui.cc/xmlrpc.php"}'
     
    In [97]: json.loads(json.dumps(dict(r.headers)))
    Out[97]:
    {u'connection': u'keep-alive',
     u'content-encoding': u'gzip',
     u'content-type': u'text/html; charset=UTF-8',
     u'date': u'Mon, 25 Jan 2016 09:56:54 GMT',
     u'server': u'nginx/1.8.0',
     u'transfer-encoding': u'chunked',
     u'x-pingback': u'http://xiaorui.cc/xmlrpc.php',
     u'x-powered-by': u'PHP/5.3.3'}
    

    相关文章

      网友评论

        本文标题:[Python]requests 得到headers解析为dic

        本文链接:https://www.haomeiwen.com/subject/cyviaftx.html