API Query - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: API Query (/thread-23642.html) |
API Query - Axel_Erfurt - Jan-09-2020 I make a query api via html. I would like to do it with python. HTML: Result: My attempt with pythonimport json import requests url = 'https://mediathekviewweb.de/api/query' headers = {'Content-Type': 'application/json'} data = ("""{ queries:[ { fields:[ 'title', 'topic' ], query:'Livestream' }, { fields:[ 'channel' ], query:'ndr' } ] }; """) response = requests.post(url, headers=headers) if response.status_code == 200: print(json.loads(response.content.decode('utf-8'))) else: print(response.status_code, response.text)The Error Message:
RE: API Query - buran - Jan-10-2020 You don't pass data in the request
RE: API Query - snippsat - Jan-10-2020 (Jan-10-2020, 03:25 AM)buran Wrote: You don't pass datain the requestI guess he forget and have tried that. It will not work the way you have it now. Most change header data content-type from the default text/html to text/plain .import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0', 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.5', 'Content-Type': 'text/plain;charset=UTF-8', 'Connection': 'keep-alive', } data = '{"queries":[{"fields":["title","topic"],"query":"Livestream"},{"fields":["channel"],"query":"ndr"}]}' response = requests.post('https://mediathekviewweb.de/api/query', headers=headers, data=data) response_json = response.json() print(response_json['result']['results'][0]['url_video'])
RE: API Query - buran - Jan-10-2020 just to add that you can do also data = {"queries":[{"fields":["title","topic"],"query":"Livestream"},{"fields":["channel"],"query":"ndr"}]} response = requests.post('https://mediathekviewweb.de/api/query', headers=headers, json=data)as an alternative to snippsat's example and get the same result RE: API Query - Axel_Erfurt - Jan-10-2020 Thank you very much. |