admin管理员组文章数量:1128537
I'm trying to fetch tweets using the twikit Python library, but I keep encountering this error:
Traceback (most recent call last):
File "c:\users\gaming\anaconda3\lib\site-packages\requests\__init__.py", line 71, in main
await get_tweets(QUERY, minimum_tweets=MINIMUM_TWEETS)
File "c:\users\gaming\anaconda3\lib\site-packages\requests\__init__.py", line 42, in get_tweets
tweets = await client.search_tweet(query, product='top')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\client\client.py", line 706, in search_tweet
response, _ = await self.gql.search_timeline(query, product, count, cursor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\client\gql.py", line 157, in search_timeline
return await self.gql_get(Endpoint.SEARCH_TIMELINE, variables, FEATURES)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\client\gql.py", line 122, in gql_get
return await self.base.get(url, params=flatten_params(params), headers=headers, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\client\client.py", line 210, in get
return await self.request('GET', url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\client\client.py", line 141, in request
await self.client_transaction.init(self.http, ct_headers)
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\x_client_transaction\transaction.py", line 34, in init
self.home_page_response = self.validate_response(home_page_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Gaming\anaconda3\Lib\site-packages\twikit\x_client_transaction\transaction.py", line 60, in validate_response
if not isinstance(response, (bs4.BeautifulSoup, requests.models.Response)):
^^^^^^^^^^^^^^^
AttributeError: module 'requests' has no attribute 'models'
This is the relevant portion of my code:
from twikit import Client
import asyncio
from configparser import ConfigParser
import csv
from datetime import datetime
from random import randint
import time
# Constants
MINIMUM_TWEETS = 30
QUERY = "chatgpt"
config = ConfigParser()
config.read(r'C:\Users\Gaming\Documents\Python Tweets\Account.ini')
USERNAME = config['x']['username']
EMAIL = config['x']['email']
PASSWORD = config['x']['password']
client = Client(language='en-us')
async def authenticate():
try:
client.load_cookies('cookies.json')
except FileNotFoundError:
await client.login(auth_info_1=USERNAME, auth_info_2=EMAIL, password=PASSWORD)
client.save_cookies('cookies.json')
async def get_tweets(query, minimum_tweets=MINIMUM_TWEETS):
tweet_count = 0
tweets = None
with open('tweets.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Count', 'Username', 'Text', 'Created_At', 'Retweets', 'Likes'])
while tweet_count < minimum_tweets:
if tweets is None:
tweets = await client.search_tweet(query, product='top')
else:
tweets = await tweets.next()
for tweet in tweets:
tweet_count += 1
tweet_data = [tweet_count, tweet.user.name, tweet.text, tweet.created_at, tweet.retweet_count, tweet.like_count]
writer.writerow(tweet_data)
if not tweets:
break
wait_time = randint(5, 10)
await asyncio.sleep(wait_time)
async def main():
await authenticate()
await get_tweets(QUERY, minimum_tweets=MINIMUM_TWEETS)
if __name__ == "__main__":
try:
loop = asyncio.get_running_loop()
if loop.is_running():
asyncio.create_task(main())
else:
asyncio.run(main())
except RuntimeError:
asyncio.run(main())
What I've tried: Ensured requests is correctly installed (pip show requests shows version 2.31.0). There are no requests.py files in the project directory that might shadow the real module. Reinstalled requests using pip install --force-reinstall requests. Question: Why is this error happening, and how can I fix the AttributeError: module 'requests' has no attribute 'models' when trying to fetch tweets with twikit?
If there is another alternative way of doing it without having to deal with twitters api limit I'm happy to try. Thanks
版权声明:本文标题:AttributeError: module 'requests' has no attribute 'models' when using Twikit in Python - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736725624a1949711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论