May-15-2020, 12:55 PM
(May-15-2020, 08:59 AM)snippsat Wrote: Look like you are usingtext
orcontent
to get data back.
json do not look that like if website give back real json.
Example:
>>> import requests >>> >>> r = requests.get('http://httpbin.org/get') >>> r.json() {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-5ebe589e-a1eff28e599b122b81950144'}, 'origin': '46.246.118.243', 'url': 'http://httpbin.org/get'} # Now can acess data like this >>> r.json()['origin'] '46.246.118.243'Not json,here usetext
orcontent
no will see \ or \\\ as as you get.
>> r.content (b'{\n "args": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encodi' b'ng": "gzip, deflate", \n "Host": "httpbin.org", \n "User-Agent": "py' b'thon-requests/2.22.0", \n "X-Amzn-Trace-Id": "Root=1-5ebe589e-a1eff28e' b'599b122b81950144"\n }, \n "origin": "46.246.118.243", \n "url": "http://' b'httpbin.org/get"\n}\n') >>> r.text ('{\n' ' "args": {}, \n' ' "headers": {\n' ' "Accept": "*/*", \n' ' "Accept-Encoding": "gzip, deflate", \n' ' "Host": "httpbin.org", \n' ' "User-Agent": "python-requests/2.22.0", \n' ' "X-Amzn-Trace-Id": "Root=1-5ebe589e-a1eff28e599b122b81950144"\n' ' }, \n' ' "origin": "46.246.118.243", \n' ' "url": "http://httpbin.org/get"\n' '}\n')
The .json attribute would be very helpful but unfortunately, as I mentioned, the json data is stored inside a javascript variable so I have to return the data as a string to allow me to use .replace()