samedi 5 octobre 2019

Python - how to store specific data from web request response

I am executing this script which makes a web request:

#! /usr/bin/env python

import requests
import sched, time
import json

from pprint import pprint

cookies = {'_globalinstancekey': '1594227/1/7JtxqlA7M68O42U73B_MWA=='}

def main():
    url = "http://transparency.hackxor.net/blog"

    r = requests.get(url, cookies=cookies)

    print "\nRequest: \n";
    print vars(r)

if __name__ == '__main__':
    main()

Output:

{'cookies': , '_content': '\n\n... comment from Black: removed, because irrelevant for this post ...\n', 'headers': {'X-Cache': 'HIT', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Age': '56', 'Server': 'nginx', 'Connection': 'keep-alive', 'Cache-Control': 'public; max-age: 60;', 'Date': 'Sat, 05 Oct 2019 19:23:34 GMT', 'Content-Type': 'text/html'}, 'url': u'http://transparency.hackxor.net/blog', 'status_code': 200, '_content_consumed': True, 'encoding': 'ISO-8859-1', 'request': , 'connection': , 'elapsed': datetime.timedelta(0, 0, 74170), 'raw': , 'reason': 'OK', '_next': None, 'history': []}

I try to save some values from the output into variables, e.g. the value of X-Cache and Age.

I tried this in main:

...
def main():
    url = "http://transparency.hackxor.net/blog"

    r = requests.get(url, cookies=cookies)

    print "\nRequest: \n";
    data = r.json()
    print data
...

But I get an error

Output:

Traceback (most recent call last):
  File "./cache.py", line 73, in <module>
    main()
  File "./cache.py", line 31, in main
    data = r.json()
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I tried it like here.




Aucun commentaire:

Enregistrer un commentaire