Reputation: 13
I wrote a simple script to check if a URL return me a JSON.
I'm using request to check if return 200 and is working. But now i need to check if the URL return me a JSON. I don't need to open it. Just check if is a JSON or not, because even if the URL returns me a 200 doesn't mean that is JSON file I need.
How can I check if result.json() is true? I tried to check len but if the site don't have JSON my script crashes.
import pandas as pd
from requests import get
lista = pd.read_csv('sites.csv', sep=',')
df = pd.DataFrame(lista, columns=['Site', 'Json'])
newdf = df.assign(Site=df['Site'].map(str) + 'Json')
for i in newdf['Site']:
result = get(i)
result.json()
Upvotes: 0
Views: 1219
Reputation: 3975
An option could be to check the response headers, which means you don't need to try and parse the response at all:
'application/json' in result.headers.get('Content-Type')
Upvotes: 2