python 2.7 - Python2.7 How do I check if a response is json (and parse it if it is)? -


i have 'requests.models.response' object parse. calling response.json() on response makes 'unicode' object.

primarily - how check whether response json?

secondarily - can parse json 'unicode' object bs4?

my code 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() 

the content type in headers:

>>> r.headers['content-type'] 'application/json' 

after getting json data, parse beautifulsoup. example:

import requests 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) 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

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -