Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们可以对RESTAPI设置条件吗_Python_Api_Rest - Fatal编程技术网

Python 我们可以对RESTAPI设置条件吗

Python 我们可以对RESTAPI设置条件吗,python,api,rest,Python,Api,Rest,我在做hackerrank测试时遇到了这个问题 问题是要找出平局的足球比赛的数量。i、 e数据[索引]['team1goals']==数据[索引]['team2goals'] 下面是一个可以使用的API: 这就是我所尝试的: 导入请求 年份=2011年 绘图=0 r=请求。获取'https://jsonmock.hackerrank.com/api/football_matches?year=“+stryear+”&page=1'.json 总页数=r[“总页数”] 每页=r[“每页”] 对于范

我在做hackerrank测试时遇到了这个问题

问题是要找出平局的足球比赛的数量。i、 e数据[索引]['team1goals']==数据[索引]['team2goals']

下面是一个可以使用的API:

这就是我所尝试的:

导入请求 年份=2011年 绘图=0 r=请求。获取'https://jsonmock.hackerrank.com/api/football_matches?year=“+stryear+”&page=1'.json 总页数=r[“总页数”] 每页=r[“每页”] 对于范围为1的页面,总页数+1: r=请求。获取'https://jsonmock.hackerrank.com/api/football_matches?year=“+stryear+”&page=”+strpage.json 尝试: 对于0范围内的i,每页: 如果intr['data'][i]['team1goals']==intr['data'][i]['team2goals']: 绘图+=1 除: 通过 打印图516 它给了我正确的答案。由于数据量很大,它面临着我不想要的时间复杂性

是否有可能,我们可以用如下条件修改REST API:


您应该使用多线程并并行发出多个请求。

您应该使用多线程并并行发出多个请求。

如果API允许这么多调用,您可以使用multiprocessing.pool.pool函数并并行迭代每个页面以减少时间。这应该起作用:

import requests
from functools import partial
from multiprocessing.pool import Pool

def loop(page,year,r,per_page):

   r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page='+str(page)).json()
   try:
      for i in range(0, per_page):
         if int(r['data'][i]['team1goals']) == int(r['data'][i]['team2goals']):
            increase = 1
         else:
            increase = 0
   except:
      increase = 0
   
   return increase

if __name__ == "__main__":
    
   year = 2011
   draw = []
   r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page=1').json()
   total_pages = r['total_pages']
   per_page = r['per_page']
   pages = range(1, total_pages+1)
   pool = Pool()
   f = pool.map(partial(loop,year=year,r=r,per_page=per_page),pages)
   draw += f
   final = 0
   for x in draw:
      x = int(x)
      final += x

   print(final) #516

如果API允许这么多调用,则可以使用multiprocessing.pool.pool函数并行遍历每个页面以减少时间。这应该起作用:

import requests
from functools import partial
from multiprocessing.pool import Pool

def loop(page,year,r,per_page):

   r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page='+str(page)).json()
   try:
      for i in range(0, per_page):
         if int(r['data'][i]['team1goals']) == int(r['data'][i]['team2goals']):
            increase = 1
         else:
            increase = 0
   except:
      increase = 0
   
   return increase

if __name__ == "__main__":
    
   year = 2011
   draw = []
   r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page=1').json()
   total_pages = r['total_pages']
   per_page = r['per_page']
   pages = range(1, total_pages+1)
   pool = Pool()
   f = pool.map(partial(loop,year=year,r=r,per_page=per_page),pages)
   draw += f
   final = 0
   for x in draw:
      x = int(x)
      final += x

   print(final) #516