python - 登录站点的 asyncio post 请求

标签 python python-3.x python-requests python-asyncio

我目前正在编写一个脚本,该脚本首先使用 cfscrape 绕过 cloudflare,然后使用有效负载发出 2 个 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'

最佳答案

BaseEventLoop.run_in_executor(executor, callback, *args)

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

  1. 使用 cfscrape 来发布数据而不是 requests ,除非您在发布请求中添加 Cookie
  2. await 必须在 async def
  3. run_in_executor 只获取 args 而不是 kwargs
  4. 规则# 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())

关于python - 登录站点的 asyncio post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020839/

相关文章:

Python3,请求 : How to merge CookieJars

python - 如果在 python 中设置,os.getenv 不能作为函数参数的默认值

python - 在旧式 Python 类上查找魔法方法

python - 在 OpenShift 上使用密码保护 django 应用程序的非常简单的方法

python - TypeError : write() argument must be str, 不是字节(Python 3 与 Python 2)

python - 一个元组的列表,其中包含多个项目

python - 可以在 ironpython 2.7.5 中使用请求吗?

http - 访问url时如何验证中间服务器是否可读用户和密码部分

python - python 中奇怪的静默崩溃与 : OSX, 请求、sqlite3、多处理的组合

python - 如何加快动画速度?