Query Twitter Status by Using Python and Tweepy - python

I try to query a specified user's tweets with a specified key word included in the tweet text. Here is my code:
# Import Tweepy, sleep, credentials.py
import tweepy
from time import sleep
from credentials import *
# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
SCREEN_NAME = "BachelorABC"
KEYWORD = "TheBachelor"
def twtr2():
raw_tweets = tweepy.Cursor(api.search, q=KEYWORD, lang="en").items(50)
for tweet in raw_tweets:
if tweet['user']['screen_name'] == SCREEN_NAME:
print tweet
twtr2()
I get the error message as below:
Traceback (most recent call last):
File "test2.py", line 19, in <module>
twtr2()
File "test2.py", line 17, in twtr2
if tweet['user']['screen_name'] == SCREEN_NAME:
TypeError: 'Status' object has no attribute '__getitem__'
I googled a lot and thought that maybe I needed to save Twitter's JSON in python first, so I tried the following:
import tweepy, json
from time import sleep
from credentials import *
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
SCREEN_NAME = "BachelorABC"
KEYWORD = "TheBachelor"
raw_tweets = tweepy.Cursor(api.search, q=KEYWORD, lang="en").items(50)
for tweet in raw_tweets:
load_tweet = json.loads(tweet)
if load_tweet['user']['screen_name'] == SCREEN_NAME:
print tweet
However, the result is sad:
Traceback (most recent call last):
File "test2.py", line 35, in <module>
load_tweet = json.loads(tweet)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
Does anyone know what's wrong with my code? And can you help me to fix it?
Thanks in advance!

I figured out. Here is the solution:
# Import Tweepy, sleep, credentials.py
import tweepy
from time import sleep
from credentials import *
# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
SCREEN_NAME = "BachelorABC"
KEYWORD = "TheBachelor"
for tweet in tweepy.Cursor(api.search, q=KEYWORD, lang="en").items(200):
if tweet.user.screen_name == SCREEN_NAME:
print tweet.text
print tweet.user.screen_name
Please do note that this is not an efficient way to locate the tweets with both specified conditions (screen_name and keyword) satisfied. This is because we query by keyword first, and then query by screen_name. If the keyword is very popular, like what I use here "TheBachelor", with a limited number of tweets (200), we may find none of the 200 tweets are sent by the specified screen_name. I think if we can query by screen_name first, and then by keyword, maybe it will provide a better result. But that's out of the discussion.
I will leave you here.

The issue is with the
load_tweet = json.loads(tweet)
The "tweet" object is not a JSON object. If you want to use JSON objects, follow this stackoverflow post on how to use JSON objects with tweepy.
To achieve what you are trying to do (print each tweet of a feed of 50), I would follow what was stated in the getting started docs:
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)

Related

Tweepy Attribute Error

I am new to python and tweepy and get an error message I cannot really understand:
import json
from tweepy import Cursor
from twitter_client import get_twitter_client
if __name__ == '__main__':
client = get_twitter_client()
with open('home_timeline.jsonl', 'w') as f:
for page in Cursor(client.home_timeline, count=200).pages(4):
for status in page:
f.write(json.dumps(status._json)+"\n")
Running this code gives the following error-messages:
Traceback (most recent call last):
File "twitter_get_user_timeline.py", line 10, in <module>
for page in Cursor(client.home_timeline, count=200).pages(4):
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/cursor.py", line 49, in __next__
return self.next()
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/cursor.py", line 108, in next
data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/binder.py", line 239, in _call
return method.execute()
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/binder.py", line 174, in execute
auth = self.api.auth.apply_auth()
AttributeError: 'function' object has no attribute 'apply_auth'
Since this goes really deep into Tweepy, I cannot really understand where my mistake in the code lies (and the code is from the book: Marco Bonzanini: "Mastering Social Media Mining with Python"). Does anybody have an idea what's going wrong here?
The authentification is done in the twitter_client that is imported. The code there is:
import os
import sys
from tweepy import API
from tweepy import OAuthHandler
def get_twitter_auth():
"""Setup Twitter authentication.
Return: tweepy.OAuthHandler object
"""
try:
consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']
access_token = os.environ['TWITTER_ACCESS_TOKEN']
access_secret = os.environ['TWITTER_ACESS_SECRET']
except KeyError:
sys.stderr.write("TWITER_* environment variables not set\n")
sys.exit(1)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
return auth
def get_twitter_client():
"""Setup Twitter API client
Return: tweepy.API object
"""
auth = get_twitter_auth
client = API(auth)
return client
Thanks a lot for any advice!
In second file, function get_twitter_client, line auth = get_twitter_auth.
You are saving the get_twitter_authfunction in auth and not the returned value.
Fix it with auth = get_twitter_auth()

Cant figure out Tweepy Twitter Search API code

I am very new to Python having taught it to myself just a few weeks ago. I have tried to cobble together some simple script using Tweepy to do various things with the Twitter API. I have been trying to get the Search API working but to no avail. I have the following code just to simply Search the last 7 days of Twitter for keywords.
# 1.Import required libs and used objects/libs
import tweepy
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor
#2. GET or input App keys and tokens. Here keys/tokens are pasted from Twitter.
ckey = 'key'
csecret = 'secret'
atoken = 'token'
asecret = 'secret'
# 3. Set authorization tokens.
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
#4. Define API.
api = tweepy.API(auth)
#5. Define list or library.
for tweets in tweepy.Cursor(api.search, q = '#IS', count = 100,
result_type ='recent').items():
print tweet.text
Every time I get the following error:
Traceback (most recent call last):
File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
result_type ='recent')).items():
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 22, in __init__
raise TweepError('This method does not perform pagination')
TweepError: This method does not perform pagination
I also tried
for tweets in tweepy.Cursor(api.search(q = '#IS', count = 100,
result_type ='recent')).items():
print tweet.text
But then I get:
Traceback (most recent call last):
File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
result_type ='recent').items():
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 181, in next
self.current_page = self.page_iterator.next()
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 101, in next
old_parser = self.method.__self__.parser
AttributeError: 'function' object has no attribute '__self__'
Can anyone please point me in the right direction, this has been driving me nuts for the past few days.
Thanks.
First of all, you are importing some things wrong, you might want to read more about how import works:
What are good rules of thumb for Python imports?
A working example of how to make this kind of search work:
import tweepy
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
ACCESS_KEY = 'accesskey'
ACCESS_SECRET = 'accesssecret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search,
q="#IS",
count=100,
result_type="recent",
include_entities=True,
lang="en").items():
print tweet.tweet
Also, I would recommend to avoid spaces in filenames, instead of "tweepy streaming.py" go for something like "tweepy_streaming.py".

python error in get trending topic using tweepy

I am trying to get top 20 trending topic through twitter api based on the Tweepy library.
Here is my python code:
import tweepy
import json
import time
today = time.strftime("%Y-%m-%d")
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_KEY = ""
ACCESS_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
trends = api.trends_daily(today)
print trends
I am using trends_daily function to get the top 20 trending topics for each day.
The variable "today" is in date format: today = time.strftime("%Y-%m-%d"). And I tried string format as well. However, it keeps report error message:
File "/Users/Ivy/PycharmProjects/TwitterTrend/trends.py", line 17, in <module>
trends = api.trends_daily("2014-06-03")
File "build/bdist.macosx-10.9-intel/egg/tweepy/binder.py", line 230, in _call
File "build/bdist.macosx-10.9-intel/egg/tweepy/binder.py", line 203, in execute
tweepy.error.TweepError: [{u'message': u'Sorry, that page does not exist', u'code': 34}]
I believe that you're using tweepy version 1 which is no longer supported: https://api.twitter.com/1/trends/daily.json
Try to re-install (version 1.1), for example:
https://api.twitter.com/1.1/trends/available.json

unable to post tweet on python 3.4

import twitter
Api = twitter.api(consumer_key='[gdgfdfhgfuff] ',
consumer_secret='[jhhjf] ',
access_token_key=' [jhvhvvhjvhvhvh]',
access_token_secret='[hvghgvvh] ')
friends=Api.PostUpdate("First Tweet from PYTHON APP ")
error given
Traceback (most recent call last):
File "C:/Python34/t7.py", line 6, in <module>
access_token_secret='[ghfghgghv] ')
TypeError: 'module' object is not callable
You have to use
from twitter.api import Api
because you are importing api module form twitter package. You have to import Api class from api module in twitter package.
....
class Api(object):
....
....
def __init__(self,
consumer_key=None,
consumer_secret=None,
access_token_key=None,
access_token_secret=None,
input_encoding=None,
request_headers=None,
cache=DEFAULT_CACHE,
shortner=None,
base_url=None,
stream_url=None,
use_gzip_compression=False,
debugHTTP=False,
requests_timeout=None):
'''Instantiate a new twitter.Api object.
Args:
......
...
You can post on twitter using this sample code
import tweepy
consumer_key = Your_consumer_key
consumer_secret = Your_consumer_secret
access_token = Your_access_token
access_token_secret = Your_access_token_secret_key
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"

Post tweet with tweepy

I'm trying to post a tweet with the tweepy library. I use this code:
import tweepy
CONSUMER_KEY ="XXXX"
CONSUMER_SECRET = "XXXX"
ACCESS_KEY = "XXXX"
ACCESS_SECRET = "XXXX"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('Updating using OAuth authentication via Tweepy!')
But when I run the application, I receive this error:
raise TweepError(error_msg, resp)
TweepError: Read-only application cannot POST.
How can I fix this?
In the application's settings, set your Application Type to "Read and Write". Then renegotiate your access token.
the code works for me with only
api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
You have to set your app to read and write
After that, you'll be able to run your code.
the following python script will tweet a line from a text file. if you want to tweet multiple tweets just put them on a new line separated by a blank line.
import tweepy
from time import sleep
# import keys from cregorg.py
from credorg import *
client = tweepy.Client(bearer_token, 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)
print("Tweet-TXT-Bot v1.0 by deusopus (deusopus#gmail.com)")
# Open text file tweets.txt (or your chosen file) for reading
my_file = open('tweets.txt', 'r')
# Read lines one by one from my_file and assign to file_lines variable
file_lines = my_file.readlines()
# Close file
my_file.close()
# Tweet a line every 5 minutes
def tweet():
for line in file_lines:
try:
print(line)
if line != '\n':
api.update_status(line)
sleep(300)
else:
pass
except tweepy.errors.TweepyException as e:
print(e)
while True:
tweet()

Categories