Twikit 库在尝试登录时返回 400

问题描述 投票:0回答:1

我正在与 Twikit 合作,从 Twitter 网站获取一些数据。但是,当我尝试登录我为执行此项目而创建的帐户时,遇到了以下错误。

这是错误:

twikit.errors.BadRequest: status: 400, message: "{"errors":[{"code":366,"message":"flow name LoginFlow is currently not accessible"}]}"

我尝试使用不同的帐户并删除计算机上的 cookie。但没有什么对我有用。

这是我到目前为止的代码:

import os
import os.path

from dotenv import load_dotenv
import asyncio
import csv

load_dotenv()

# Import credentials
USERNAME = os.getenv("USERNAME")
PASSWORD = os.getenv("PASSWORD")
EMAIL = os.getenv("EMAIL")

client = Client("en-US")

async def login():
   await client.login(
       auth_info_1=USERNAME,
       auth_info_2=EMAIL,
       password=PASSWORD
   )


async def search_and_download(key_word):
   query = f"{key_word} lang:en -login -support since:2023-01-01 until:2024-10-01"
   
   tweets = await client.search_tweet(
       query=query
   )

   # Save tweets in csv file
   file_path = 'files/raw/twitter/tweets.csv'
   file_exists = os.path.isfile(file_path)

   with open(file_path, mode='a', newline='', encoding='utf-8') as file:
       writer = csv.writer(file)
       if not file_exists:
           writer.writerow(['id', 'user', 'text', 'created_at'])

       # Process and save tweets
       print(f"Downloading tweets for: {key_word}...")
       for tweet in tweets:
           writer.writerow([tweet.id, tweet.user.name, tweet.text, tweet.created_at])

   print(f"Tweets saved for keyword: {key_word}")


async def main():
   await login()

   # Group similar key words and include sentiment-related terms
   key_words_files = ["binance_trends.csv", "bitcoin_trends.csv",  "cardano_trends.csv",  "crypto_trends.csv",  "ethereum_trends.csv"]
   key_words = []

   for file in key_words_files:
       with open("keywords/"+ file, mode='r', encoding='utf-8') as file:
           reader = csv.reader(file)
           for row in reader:
               key_words.append(row[0])
   
   key_words = [kw for kw in key_words if kw not in ["keywords", ""]]

   # Additional sentiment-related terms
   sentiment_terms = [
       "profit", "loss", "bullish", "bearish", "crash", "rise", "sell-off", "moon", "panic"
   ]

   # Group key words with sentiment-related terms
   extended_key_words = []
   for kw in key_words:
       for sentiment in sentiment_terms:
           extended_key_words.append(f"{kw} {sentiment}")
       extended_key_words.append(kw)

   # Search and download tweets for every key word with sentiment context
   for key_word in extended_key_words:
       await search_and_download(key_word, "Latest")


if __name__ == "__main__":
   asyncio.run(main())

有什么建议可以解决这个问题吗?

此外,这段代码之前可以运行,没有任何问题。这个问题是几天前开始的。

python web-scraping twitter
1个回答
0
投票

鉴于此方法之前有效,但几天前突然停止工作,Twitter 可能对其身份验证系统进行了一些 Twikit 库尚未适应的更改。 我可以想到一些建议:

  • 检查 Twikit 的 GitHub 存储库或文档以了解 API 中的更改 查找有关此错误的任何最新问题或公告。 开发人员可能正在解决一个已知问题。

  • 更新 Twikit 或使用不同的 Twitter API 库:考虑切换到更积极维护的库,如 Tweepy 或
    TwitterAPI。这些较大的项目可能会更快地适应
    Twitter 的变化。

  • 检查速率限制:确保您没有达到 Twitter 施加的任何速率限制,因为这有时会导致意外情况
    错误。

  • 进一步调试: 将日志记录添加到代码中以捕获有关所发出的请求和响应的更多详细信息 已收到。这可以帮助诊断问题。”

希望它能有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.