Astrophe Astrophe - 22 days ago 6
JSON Question

Python2.7 If response is json how do I parse it?

I have a 'requests.models.Response' object which I would like to parse. Calling response.json() on the response makes a 'unicode' object.

Primarily - How do I check whether the response is json?

Secondarily - Can I parse a json 'unicode' object with bs4?

My code is as follows:

import requests

post_hdrs = {
'type': 'regulated',
'url': 'node/17'
}

r = requests.post(
url='https://www.gfsc.gg/fetch-records-for-companies-table',
data=post_hdrs,
)

json_data = r.json()

Answer

The content type is in the headers:

>>> r.headers['Content-Type']
'application/json'

After getting the json data, parse it with BeautifulSoup. Example:

import requests
from bs4 import BeautifulSoup

post_hdrs = { 
    'type': 'regulated',
    'url': 'node/17'
}

r = requests.post(
    url='https://www.gfsc.gg/fetch-records-for-companies-table',
    data=post_hdrs,
)

print r.headers['Content-Type']
print
data = r.json()
soup = BeautifulSoup(data)
for c in soup.findAll('td',attrs={'class':'Company Name'}):
    print c.text

Output:

application/json

2Mi Financial Services Limited
71FS Insurance Company Limited
7L Capital Partners Emerging Europe L.P.
7L Equity Partners (EE) Limited
   :    :    :
Comments