I'm trying to get status updates from a list in Twitter, then open in a CSV file, but I keep getting the following error:
AttributeError: 'Status' object has no attribute 'screen_name'.
Any suggestions?
import tweepy
from tweepy import OAuthHandler
import csv
import pandas as pd
consumer_key = 'x'
consumer_secret = 'x'
access_token = 'x'
access_secret = 'x'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
f = csv.writer(open('TodaysNews.csv', 'w'))
f.writerow(["screenName", "name", "text", "followers_count", "listed_count", "statuses_count"])
number_of_tweets = 100
tweets_for_csv = []
for tweet in tweepy.Cursor(api.list_timeline, 'darrenmeritz', 'News',
twtHandle = tweet.screen_name,
name = tweet.name,
text = tweet.text,
followers_count = tweet.followers_count,
listed_count = tweet.listed_count,
statuses_count = tweet.statuses_count,
result_type='recent',
include_entities=True,
trim_user=True,
truncated=False,
lang='en').items(number_of_tweets):
try:
f.writerow([twtHandle, name, text, followers_count, listed_count, statuses_count])
except UnicodeEncodeError:
pass
Related
I can retrieve tweets with a specific hashtag using tweepy:
Code:
from os import access
import tweepy
import configparser
import pandas as pd
# config = configparser.ConfigParser()
# config.read('config.ini')
api_key = ''
api_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# user = '#veritasium'
keywords = '#SheHulk'
limit = 1200
tweets = tweepy.Cursor(api.search_tweets, q = keywords, count = 100, tweet_mode = 'extended').items(limit)
columns = ['User', 'Tweet']
data = []
for tweet in tweets:
data.append([tweet.user.screen_name, tweet.full_text])
df = pd.DataFrame(data, columns=columns)
df.to_excel("output.xlsx")
What I want to know is that if I can get the number of likes with every tweet that is retrieved. Any help would be appreciated.
In the Twitter API V1.1 (see documentation here), that field was called favorite_count.
for tweet in tweets:
print(f"That tweet has {tweet.favorite_count} likes").
I'm trying to get tweets from a certain hashtag between a defined amount of time. The code I've compiled is not working due to the error specified below:
AttributeError: 'SearchResults' object has no attribute 'items'
Below is the code:
import tweepy
import csv
import pandas
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
csvFile = open('ua.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search_tweets,
q = "#myquery",
since = "2022-01-01",
until = "2022-02-01",
lang = "en").items():
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])
print tweet.created_at, tweet.text
csvFile.close()
Error code in detail:
File "...", line 20, in <module>
for tweet in api.search_tweets(q="#myquery").items():
AttributeError: 'SearchResults' object has no attribute 'items'
Any help appreciated.
I am getting error : help me I tried many times. But it's not showing id from username.
for tweet in tweepy.Cursor(api..........:
try:
screen_name= "NFTfemmefatale"
id = screen_name
get = api.get_user(id)
print("id:" + str (get))
except:
print("error")
try this:
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the screen name of the user
screen_name = "yourname"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)
With the python code below I tried to fetch 3200 tweets from a public twitter profile, but so far I only get different amounts of tweets which are way less than the maximum of 3200 tweets and I can't really understand the problem. Can someone please explain me what I am doing wrong here?
import tweepy
import json
import pandas as pd
consumer_key = "xxx"
consumer_secret = "xxx"
access_token = "xxx"
access_token_secret = "xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
results=[]
timeline = tweepy.Cursor(api.user_timeline, screen_name='#realDonaldTrump', tweet_mode="extended").items()
for status in timeline:
data = (
status.user.id,
status.user.screen_name,
status.user.name,
status.full_text,
status.created_at,
status.lang)
results.append(data)
cols = "user_id screen_name name text date lang".split()
df = pd.DataFrame(results, columns=cols)
i'm trying to get data from twitter using
tweepy library on python i get this error i already tried some solutions like changing the Keys but still not working
import tweepy
import xlsxwriter
from TweetClassifier import TweetClassifier
from DataCleaner import DataCleaner
import pandas as pd
import os
from tweepy import OAuthHandler
class TwitterAPI:
tweets = None
query = None
number_of_tweets = 100
date = None
consumer_key = "vxxxxxxxxxxxxxxxx6"
consumer_secret = "Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxvv"
access_token = "295xxx24-eYxxxxxxerE9"
access_secret = "V2xxxxxxxxWadL"
data_clean = DataCleaner()
tweets_classifier = TweetClassifier()
def __init__(self):
return
def Auth(self):
auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self. access_token, self.access_secret)
api = tweepy.API(auth)
return api
def retrieve_tweets(self, query, api):
tweets = []
for tweet in tweepy.Cursor(api.search, q=query).items(100):
tweets.append(tweet.text)
return tweets
error