Python 登录到站点的asyncio post请求

Python 登录到站点的asyncio post请求,python,python-3.x,python-requests,python-asyncio,Python,Python 3.x,Python Requests,Python Asyncio,我目前正在编写一个脚本,该脚本首先使用cfscrape绕过cloudflare,然后使用有效负载发出两个post请求以登录站点。我在future1和future2的文章中遇到了一些错误。这是我的密码: import asyncio import requests import cfscrape async def main(): s = requests.Session() s.get('https://www.off---white.com/en/IT') headers = {

我目前正在编写一个脚本,该脚本首先使用cfscrape绕过cloudflare,然后使用有效负载发出两个post请求以登录站点。我在future1和future2的文章中遇到了一些错误。这是我的密码:

import asyncio
import requests
import cfscrape

async def main():
s = requests.Session()
s.get('https://www.off---white.com/en/IT')

headers = {
    'Referer': 'https://www.off---white.com/it/IT/login',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
    }

payload1 = {
    'spree_user[email]': 'email',
    'spree_user[password]': 'password',
    'spree_user[remember_me]': '0',
}

payload2 = {
    'spree_user[email]': 'email',
    'spree_user[password]': 'password',
    'spree_user[remember_me]': '0',
}

scraper = cfscrape.create_scraper(s)
scraper.get('https://www.off---white.com/en/IT', headers=headers)
print('Done')

loop = asyncio.get_event_loop()
print('Starting loop')

future1 = loop.run_in_executor(None, requests.post ,'https://www.off---white.com/it/IT/login', data=payload1, headers=headers)
future2 = loop.run_in_executor(None, requests.post ,'https://www.off---white.com/it/IT/login', data=payload2, headers=headers)
response1 = await future1
response2 = await future2
print(response1.text)
print(response2.text)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
错误:

File "async_amatriciana.py", line 41, in <module>
loop.run_until_complete(main())
File "lib/python3.6/asyncio/base_events.py" line 468, in 
run_until_complete
return future.result()
File "async_amatriciana.py", line 33, in main
future1 = loop.run_in_executor(None, requests.post ,'https://www.off--- 
white.com/it/IT/login', data=payload1, headers=headers)
TypeError: run_in_executor() got an unexpected keyword argument 'data'
文件“async_amatriciana.py”,第41行,在
循环。运行\u直到完成(main())
文件“lib/python3.6/asyncio/base_events.py”第468行,在
运行直到完成
返回future.result()
文件“async_amatriciana.py”,第33行,主目录
future1=loop.run_in_executor(无,requests.post,'https://www.off--- 
white.com/it/it/login',data=payload1,headers=headers)
TypeError:run_in_executor()获得意外的关键字参数“data”
BaseEventLoop.在执行器中运行执行器(执行器、回调、*args)

我运行了你的代码,并得到了很多错误,因此我重写了你的代码。你需要知道如何遵循这些

  • 使用
    cfscrape
    发布数据,而不是
    请求
    ,除非您将cookie添加到发布请求中
  • wait
    必须在
    async def
  • run\u in\u executor
    仅获取
    args
    kwargs
  • 规则9:不要在异步代码中使用
    请求
    ——来自@Brad Solomon
  • 重写的代码

    import asyncio
    import requests
    import cfscrape
    
    headers = {
        'Referer': 'https://www.off---white.com/it/IT/login',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        }
    
    payload1 = {
        'spree_user[email]': 'email',
        'spree_user[password]': 'password',
        'spree_user[remember_me]': '0',
    }
    
    payload2 = {
        'spree_user[email]': 'email',
        'spree_user[password]': 'password',
        'spree_user[remember_me]': '0',
    }
    
    
    def post(dict):
        scraper = cfscrape.create_scraper(requests.Session())
        req = scraper.post(**dict)
        return req
    
    async def get_data():
        datas = [dict(url='https://www.off---white.com/it/IT/login', data=payload1, headers=headers),
                dict(url='https://www.off---white.com/it/IT/login', data=payload2, headers=headers)]
        loop = asyncio.get_event_loop()
        response = [loop.run_in_executor(None, post , data) for data in datas]
        result = await asyncio.gather(*response)
        print(result)
    
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(get_data())
    

    错误是什么?请将您的问题包括在内。此外,您不能在使用
    async def
    @bradsomon定义的协同程序之外使用
    wait
    ,最大的困难是使用off white have cloudflare,您是对的
    请求
    是块模块,但使用
    run_in_executor
    时是多线程。我认为这并没有错,就像在Tornado上运行阻塞函数一样,多亏了all,最后我选择了用线程来编写脚本,而不是使用asyncio。@ChefTraian在您的代码中,您也使用了线程。但这是并发的.future类型。