I am trying to use the python twitter API but it can't find the Twitter class.
https://pypi.python.org/pypi/twitter
For instance I do the following without any errors:
from twitter import *
But when I do:
>>> t = Twitter(auth)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Twitter' is not defined
What am I missing? Is there a different python twitter library I need to install?
Try using the tweepy library, easy to use. Take a look at http://www.tweepy.org/
Here is a code example:
import tweepy
auth = tweepy.OAuthHandler('','')
auth.set_access_token('', '')
api = tweepy.API(auth)
public_tweets = api.followers_ids()
for tweet in public_tweets:
print len(public_tweets)
Related
I am trying to run the code from https://github.com/CreepyD246/Twitter-Reply-Bot/blob/main/TwitterReplyBot.py on Replit.
#Importing modules/libraries
import tweepy
import time
import os
# Initialization code
auth = tweepy.OAuthHandler(os.getenv("key2"),os.getenv("skey2"))
auth.set_access_token(os.getenv("token2"),os.getenv("stoken2"))
api = tweepy.API(auth)
# Some important variables which will be used later
bot_id = int(api.me().id_str)
#Other things after
But I get this error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
bot_id = int(api.me().id_str)
AttributeError: 'API' object has no attribute 'me'
I don't find anything in the Tweepy's documentation about API.me() so what's wrong?
The Tweepy version is 4.1.0.
Tweepy v4.0.0 removed API.me.
You can use API.verify_credentials instead.
Tweepy v4.0.0 removed API.me use API.get_user
That is the replacement for now
yes api.get_user is the replacement in tweepy v4.0.0
This worked for me api.get_user(screen_name="replaceWithYourTwitterUsername")
So I'm currently working on a script using Shodan in Python using import shodan.
The documentation around isn't the best so id thought I'd bring my issue here.
Using the Shodan CLI there is a command known as Shodan Info which displays the number of search credits that your account has remaining.
When looking at the Shodan Documentation i can see that there is an info() tag but whenever I use it there has been an error.
Does anyone know the correct syntax for using info in a python script to display the number of credits an account has?
Errors i have received:
ipinfo = shodan.info()
print (ipinfo)
Error
Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XX Documents/XXXX/Working
Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.info()
AttributeError: module 'shodan' has no attribute 'info'
And
ipinfo = shodan.Shodan(info())
print (ipinfo)
Error
Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XXXXDocuments/XXXXXX/Working
Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.Shodan(info())
NameError: name 'info' is not defined
You need to instantiate the Shodan class and then you will be able to use its Shodan.info() method. Here is how it looks in the official command-line interface:
https://github.com/achillean/shodan-python/blob/master/shodan/__main__.py#L364
Here's a short script that prints the current account usage information:
import pprint
import shodan
key = 'YOUR API KEY'
api = shodan.Shodan(key)
results = api.info()
pprint.pprint(results)
shodan does not express how to use info. heres a breif example of how to set up shodan info
import shodan
api = shodan.Shodan('YOUR API KEY')
info = api.host('8.8.8.8')
shodan info
Under python2.7 and already installed the requirements.txt from the twitter-python Building section
It's the first time I'm jumping in, and following the basics steps to ensure is everything okay as described here in this link twitter-python Documentation section, I'm getting an error.
Here the typen at command line Python shell:
>>> api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
The error:
>>> print api.VerifyCredentials()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ubuntu/.virtualenvs/twitter/local/lib/python2.7/site- packages/python_twitter-1.2-py2.7.egg/twitter.py", line 5209, in VerifyCredentials
data = self._ParseAndCheckTwitter(json.content)
File "/home/ubuntu/.virtualenvs/twitter/local/lib/python2.7/site-packages/python_twitter-1.2-py2.7.egg/twitter.py", line 5462, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "/home/ubuntu/.virtualenvs/twitter/local/lib/python2.7/site-packages/python_twitter-1.2-py2.7.egg/twitter.py", line 5487, in _CheckForTwitterError
raise TwitterError(data['errors'])
twitter.TwitterError: [{u'message': u'Invalid or expired token', u'code': 89}]
What could I be missing here?
You need to replace 'consumer_key', 'consumer_secret', 'access_token', and 'access_token_secret' with their actual values. You can either put those values directly in the twitter.Api() call, or assign their values to variables:
>>> # all these values are just random, you'll need to use your own values
>>> c_key = '123456'
>>> c_secret = 'a88d098cd76'
>>> token = '98765'
>>> token_secret = 'ad98c63e87f00'
>>> api = twitter.Api(consumer_key=c_key,
consumer_secret=c_secret,
access_token_key=token,
access_token_secret=token_secret)
I'm not sure but it looks like you need a valid consumer_key, consumer_secret, access_token_key and access_token_secret. This will involve you setting up your own app on twitter.com and using the consumer keys/secrets and test access_token keys/secrets to get started.
I am trying to develop a gtk3 desktop application using python to perform the basic twitter functions like accessing the home timeline of a user, making tweets etc.
I am using python-twitter library, but am unable to find the API call for the purpose. I checked and saw there were a few patches , but they dont seem to work. the rest of the functions I am able to accomplish using the library.
I need help!!!
[edit]
this is the error i am facing when i tried using a fork of the python-twitter library, as given on: http://github.com/jaytaylor/python-twitter-api
Error:
>>api.getUserTimeline('gaurav_sood91')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "twitter.py", line 2646, in getUserTimeline
self._checkForTwitterError(data)
File "twitter.py", line 3861, in _checkForTwitterError
if data.has_key('next_cursor'):
AttributeError: 'list' object has no attribute 'has_key'
Using the python-twitter module from code.google.com, documentation here.
Accessing user timelines:
import twitter
api = twitter.Api()
statuses = api.GetUserTimeline('#gaurav_sood91')
print [s.text for s in statuses]
Posting tweets:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
status = api.PostUpdate('This is my update text.')
Edit for applying GetHomeTimeline patch:
Disclaimer: I'm on Windows, so you may need to change these steps a bit.
Download python-twitter
Extract to folder
Download 0002-Support-for-home-timeline.patch file from issue 152
Copy/move patch file to root of extracted python-twitter directory (there should be a file named twitter.py in this dir)
Run command: patch twitter.py 0002-Support-for-home-timeline.patch, you should get a message that patch succeeded
In same directory, run command: python setup.py install
Run interactive python shell: import twitter, dir(twitter.Api)
You should see the GetHomeTimeline method listed.
Update for GetHomeTimeline:
Found patch in issue 152 that works well using OAuth and JSON parse method that is now part of Status class. Sample code:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
statuses = api.GetHomeTimeline()
print [s.text for s in statuses]
I'm trying to use the python-twitter module, but still having problems initiating the twitter.Api(). I've checked and rechecked that no other file named twitter.py or twitter.pyc is on my system. On a clean install i first try to
>>> import twitter
and correctly get a response of 'module unknown'
I do a easy_install twitter, successfull.
Then do
>>> import twitter
>>> testapi = twitter.Api()
The response is
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
testapi = twitter.Api()
AttributeError: 'module' object has no attribute 'Api'
>>>
I'm exhausted trying to locate the problem here, please help.
It appears that you are trying to follow the documentation for one Python Twitter module when you are in fact using another Python Twitter module.
The Api() method call you mention is part of this Python Twitter module. However, when you use easy_install twitter, you actually get this other Python Twitter module.