Jan-09-2020, 08:37 PM
I make a query api via html.
I would like to do it with python.
HTML:
Result:
I would like to do it with python.
HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MediathekViewWeb API</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<script>
let query = {
queries:[
{
fields:[
'title',
'topic'
],
query:'Livestream'
},
{
fields:[
'channel'
],
query:'ndr'
}
]
}
let queryString = JSON.stringify(query);
let request = new XMLHttpRequest();
let requestURL = 'https://mediathekviewweb.de/api/query';
request.open("POST", requestURL);
request.addEventListener('load', function(event) {
let response;
try {
response = JSON.parse(request.responseText);
} catch (e) { }
if (request.status == 200 && typeof response != 'undefined') {
for (let i = 0; i < response.result.results.length; i++) {
let entry = response.result.results[i];
let row = $('<tr>');
row.append($('<td>').text(entry.url_video));
$('#mediathek > tbody').append(row);
}
$('#responseText').text(JSON.stringify(response, null, 2));
} else {
if (response) {
console.log(response.err);
$('#errorText').html(response.err.join('</br>'));
}
else {
$('#errorText').html(request.statusText + '</br>' + request.responseText);
}
}
});
request.send(queryString);
</script>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
th {
text-align: left;
}
</style>
<body>
<table id="mediathek" class="table table-striped table-hover">
<thead>
<tr>
<th>URL</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p id="errorText"></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>MediathekViewWeb API</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<script>
let query = {
queries:[
{
fields:[
'title',
'topic'
],
query:'Livestream'
},
{
fields:[
'channel'
],
query:'ndr'
}
]
}
let queryString = JSON.stringify(query);
let request = new XMLHttpRequest();
let requestURL = 'https://mediathekviewweb.de/api/query';
request.open("POST", requestURL);
request.addEventListener('load', function(event) {
let response;
try {
response = JSON.parse(request.responseText);
} catch (e) { }
if (request.status == 200 && typeof response != 'undefined') {
for (let i = 0; i < response.result.results.length; i++) {
let entry = response.result.results[i];
let row = $('<tr>');
row.append($('<td>').text(entry.url_video));
$('#mediathek > tbody').append(row);
}
$('#responseText').text(JSON.stringify(response, null, 2));
} else {
if (response) {
console.log(response.err);
$('#errorText').html(response.err.join('</br>'));
}
else {
$('#errorText').html(request.statusText + '</br>' + request.responseText);
}
}
});
request.send(queryString);
</script>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
th {
text-align: left;
}
</style>
<body>
<table id="mediathek" class="table table-striped table-hover">
<thead>
<tr>
<th>URL</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p id="errorText"></p>
</body>
</html>
Result:
Output:URL
http://ndrfs-lh.akamaihd.net/i/ndrfs_nds@430233/master.m3u8
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:
Output:400 {"result":null,"err":["Unexpected token o in JSON at position 1"]}