Python aiohttp.ClientSession不';我似乎没有发布我提供的数据(用户名、密码、登录名)
我一直在尝试将我的一个程序变为异步,但会话似乎没有发布数据Python aiohttp.ClientSession不';我似乎没有发布我提供的数据(用户名、密码、登录名),python,python-3.x,python-requests,aiohttp,Python,Python 3.x,Python Requests,Aiohttp,我一直在尝试将我的一个程序变为异步,但会话似乎没有发布数据 from aiohttp import ClientSession as Session from bs4 import BeautifulSoup as BS import asyncio class Finder(Session): async def login(self, loginurl, homeurl, username, password): async def getlogintoken(lo
from aiohttp import ClientSession as Session
from bs4 import BeautifulSoup as BS
import asyncio
class Finder(Session):
async def login(self, loginurl, homeurl, username, password):
async def getlogintoken(loginurl):
async with self.get(loginurl) as loginpage:
return(BS(await loginpage.text(), 'html.parser').select('#login > input[type=hidden]:nth-child(3)')[0]['value'])
async def postlogindata(loginurl, username, password, logintoken):
await self.post(loginurl, data={'username': username, 'password': password, 'logintoken': logintoken})
async def gethome(homeurl):
async with self.get(homeurl) as homepage:
print(homepage.url)
logintoken = await getlogintoken(loginurl)
print(logintoken)
await postlogindata(loginurl, username, password, logintoken)
await gethome(homeurl)
这就是我所拥有的,它可以很好地处理请求(没有所有异步的东西),但是对于aiohttp它似乎不起作用。
任何帮助都将不胜感激 我发现了问题所在: ClientSession不会自动保存Cookie。它可以正确地发布,但为了保持会话ID,您必须在获取logintoken时更新Cookie
async with self.get(loginurl) as loginpage:
self.cookie_jar.update_cookies(loginpage.cookies)
return BS(await loginpage.text(), 'html.parser').find(attrs={'name': 'token'})['value']