使用twscrape库登录失败:AsyncClient.__init__()收到意外参数'proxy'
解决twscrape登录报错:AsyncClient.init() got an unexpected keyword argument 'proxy'
问题原因
这个错误是由于twscrape依赖的httpx库版本更新后API参数发生变更:旧版httpx允许直接向AsyncClient传入proxy参数,但新版httpx(0.25及以上)已将该参数替换为proxies,而twscrape的代码未同步适配这一变化,导致调用时触发参数不匹配报错,进而造成账户登录失败、无活跃账户可用。
解决方案
方案1:锁定兼容的httpx版本
在PyCharm虚拟环境中执行以下命令,安装与twscrape兼容的httpx旧版本:
pip install httpx==0.24.1
该版本保留了proxy参数的支持,可直接解决参数不匹配问题。
方案2:重建虚拟环境(方案1无效时尝试)
若虚拟环境存在依赖冲突,可彻底重建环境:
- 删除当前的
venv文件夹 - 重新创建虚拟环境:
python -m venv venv - 激活虚拟环境:
- Windows:
venv\Scripts\activate - Linux/macOS:
source venv/bin/activate
- Windows:
- 依次安装指定版本的httpx和twscrape:
pip install httpx==0.24.1 pip install twscrape
原问题代码
import asyncio from twscrape import API from twscrape.logger import set_log_level async def main(): api = API() await api.pool.add_account('username', 'user_pass', 'email','email_pass') await api.pool.login_all() async for tweet in api.search("BTC"): print(tweet.id, tweet.user.username, tweet.rawContent) set_log_level("DEBUG") if __name__ == "__main__": asyncio.run(main())
原错误信息
2024-05-03 19:10:24.384 | INFO | twscrape.db:migrate:96 - Running migration to v1 2024-05-03 19:10:24.429 | INFO | twscrape.db:migrate:96 - Running migration to v2 2024-05-03 19:10:24.505 | INFO | twscrape.db:migrate:96 - Running migration to v3 2024-05-03 19:10:24.555 | INFO | twscrape.db:migrate:96 - Running migration to v4 2024-05-03 19:10:24.695 | INFO | twscrape.accounts_pool:add_account:110 - Account <username> added successfully (active=False) 2024-05-03 19:10:24.704 | INFO | twscrape.accounts_pool:login_all:183 - [1/1] Logging in njex_zzz211 - razzrrt211@gmail.com 2024-05-03 19:10:42.962 | ERROR | twscrape.accounts_pool:login:165 - Failed to login 'username': AsyncClient.__init__() got an unexpected keyword argument 'proxy' 2024-05-03 19:10:43.029 | WARNING | twscrape.accounts_pool:get_for_queue_or_wait:297 - No active accounts. Stopping... 2024-05-03 19:10:43.045 | WARNING | twscrape.accounts_pool:get_for_queue_or_wait:297 - No active accounts. Stopping...
内容的提问来源于stack exchange,提问作者R.H He
相关产品推荐
相关产品推荐

