Python中的API请求后异步

2024-06-16 14:21:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图为开始20后,要求到下面的网址,这样我就可以得到正确的负载从每个页面的信息。我已经有了同步的代码,但我正在尝试使它异步。现在对于所有的页面,大约需要33个小时(7967页)。你知道吗

我已经尝试更改协议“https”、“http”,并检查字典列表中的数据,使其仅为字典而不是列表。你知道吗

def login_request():


  global jar
  payload = {
            'email': email,
            'password': password,
        }

  r = requests.post('https://www.explorebit.io/api/auth/signin', data=payload)

  cookies = r.headers['set-cookie']
  cookies = cookies.split('=')
  cookies = str(cookies[1].split(' ')[0])

  jar = requests.cookies.RequestsCookieJar()
  jar.set('sessionId', cookies, domain='https://www.explorebit.io', path='/api/company/getAllCompanies')


async def mine_all():

  with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
    global jar
    loop = asyncio.get_event_loop()

    data = [dict(url='https://www.explorebit.io/api/company/getAllCompanies',
                  cookies=jar, json=info, data=payload_companies, headers=headers, allow_redirects=False)]

    futures = [loop.run_in_executor(executor, requests.post, data) for i in range(20)]

    for response in await asyncio.gather(*futures):
        print(response.json())

login_request()
loop = asyncio.get_event_loop()
loop.run_until_complete(mine_all())

该网站正在使用cookies,所以首先我登录保存cookies并继续请求所有其他页面的json信息。你知道吗


Tags: httpsioloopapidatawww页面requests