使用Python请求从网站API过滤json响应

时间:2017-01-25 10:57:29

标签: python json get python-requests

我需要使用他们的API查询博彩交易网站。我可以使用以下代码获取Python请求的事件名称和ID列表:

r15 = s.get('https://www.matchbook.com/edge/rest/events/?sport-ids=24735152712200&category-ids=10812641776701')
data1 = r15.json()
for event in data1['events']:
    print(event['name'])
    print(event['id'])

我现在知道我需要按类别ID和sport-id过滤的数字,但我不知道如何过滤json响应只是为了跑步者的名字。如何使用请求过滤json响应,以便它只显示运行者名称,而不是整个json数据流?

下面给出了类似请求的json响应示例:

<events-response>
<offset>0</offset>
<per-page>1</per-page>
<total>38</total>
<events>
<event>
<id>393986311580028</id>
<name>12:50 Gowran Park</name>
<sport-id>24735152712200</sport-id>
<start>2017-01-26T12:50:00Z</start>
<in-running-flag>false</in-running-flag>
<allow-live-betting>false</allow-live-betting>
<category-id>
<category-id>10812641776701</category-id>
<category-id>24735152712200</category-id>
<category-id>24735153536300</category-id>
<category-id>393798405520028</category-id>
</category-id>
<status>open</status>
<volume>938.65922</volume>
<markets>...</markets>
<meta-tags>
<meta-tag id="10812641776701">
<name>Ireland</name>
<type>COUNTRY</type>
<url-name>ireland</url-name>
</meta-tag>
<meta-tag id="24735152712200">
<name>Horse Racing</name>
<type>SPORT</type>
<url-name>horse-racing</url-name>
</meta-tag>
<meta-tag id="24735153536300">
<name>Gowran Park</name>
<type>LOCATION</type>
<url-name>gowran-park</url-name>
</meta-tag>
<meta-tag id="393798405520028">
<name>2017-01-26</name>
<type>DATE</type>
<url-name>2017-01-26</url-name>
</meta-tag>
</meta-tags>
</event>
</events>
</events-response>

一个xml示例与我得到的json响应具有相同的结构(即以'runners'标记为特征),给出了;

https://www.matchbook.com/edge/rest/events/?sport-ids=24735152712200&category-ids=10812638253700

1 个答案:

答案 0 :(得分:1)

if((document.getElementById('Radio1id').checked) && document.getElementById('name').value != "") {
 //Action for checked radio button and text box without value
}else if(document.getElementById('Radio2id').checked) {
 //Another Action . . . 
}

if((document.getElementById('Radio1id').checked == false) && document.getElementById('name').value != "") {
 //Action for NON checked radio button and text box without value
}

出:

import requests

r = requests.get('https://www.matchbook.com/edge/rest/events')
data = r.json()
for event in data['events']:
    print(event['category-id'])