I am new to python and I have been making codes to scrap twitter data on python.
Below are my codes:
import csv
import json
import twitter_oauth
import sys
sys.path.append("/Users/jdschnieder/Documents/Modules")
print sys.path
#gain authorization to twitter
consumer_key = 'xdbX1g21REs0MPxofJPcFw'
consumer_secret = 'c9S9YI6G3GIdjkg67ecBaCXSJDlGw4sybTv1ccnkrA'
get_oauth_obj = twitter_oauth.GetOauth(consumer_key, consumer_secret)
get_oauth_obj.get_oauth()
the error occurs at the line:
get_oauth_obj.get_oauth()
the error message is as follows:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-36-5d124f99deb6> in <module>()
--> 1 get_oauth_obj.get_oauth()
/Users/jdschnieder/anaconda/python.app/Contents/lib/python2.7/site-packages/
twitter_oauth-0.2.0-py2.7.egg/twitter_oauth.pyc in get_oauth(self)
95 resp, content = client.request(_REQUEST_TOKEN_URL, "GET")
96 if resp['status'] != '200':
-> 97 raise Exception('Invalid response %s' % resp['status'])
98
99 request_token = dict(self._parse_qsl(content))
Exception: Invalid response 401
why is this error occurring, and what are possible solutions to the error?
thank you,
It looks like the Twitter library you're using does not support Twitter API v1.1. Instead of twitter_oauth, use one of the Python libraries listed here. For example, you can use Tweepy, following the documentation for OAuth Authentication
Related
I'm trying to get Tweets using the Tweepy module in Python. However, whenever I try to collect Tweets using the tweepy.Cursor function, it returns the error:
TweepyException Traceback (most recent call last)
<ipython-input-5-a26c992842dc> in <module>
----> 1 tweets_list = tweepy.Cursor(api.search_tweets(q="oranges", tweet_mode='extended', lang='en')).items()
~\anaconda3\lib\site-packages\tweepy\cursor.py in __init__(self, method, *args, **kwargs)
38 raise TweepyException('Invalid pagination mode.')
39 else:
---> 40 raise TweepyException('This method does not perform pagination')
41
42 def pages(self, limit=inf):
TweepyException: This method does not perform pagination
I do not know why this is. I can still post Tweets using the api.update_status() function. Please help. Here is my code. As you can see, the setup is correct; it is only this function that is returning the error.
from config import *
import tweepy
import datetime
consumer_key= 'CONSUMER KEY HERE'
consumer_secret= 'CONSUMER KEY SECRET HERE'
access_token= 'ACCESS TOKEN HERE'
access_token_secret= 'ACCESS TOKEN SECRET HERE'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication Successful")
except:
print("Authentication Error")
tweets_list = tweepy.Cursor(api.search_tweets(q="oranges", tweet_mode='extended', lang='en')).items()
If there is an error in the function or the code itself, can you please write a correct version? I'm trying to gather Tweet data for a project.
You're passing the result of the call to / invocation of API.search_tweets, rather than the method itself, to Cursor.
See Tweepy's Pagination documentation for an example of how to use Cursor.
Background - I am trying to create a Twitter bot which listens in real time for certain keywords coming only from certain accounts. When these conditions are met, I would then retweet it with my app, so I can follow with notifications from my main account.
Problem - Sometimes, but not always, I will encounter an error while the script is running which gives me the error listed in the subject of this post. I'm unsure what causes it, since it happens intermittently.
What I've Done - I've searched for the error; most of what I've found refers to "base 10", not "base 16". For the couple cases I've found where it references base 16, I don't understand the solution well enough to adapt it to my code (self teaching myself on this project).
Code
import tweepy
import json
import re
import logging
auth = tweepy.OAuthHandler("xxxx", "xxxx")
auth.set_access_token("yyyyy", "yyyy")
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
self.me = api.me()
def on_status(self, tweet):
keyword = ["Keyword1", "Keyword2"]
accounts = ['account1','account2']
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in keyword]
patterns2 = [r'\b%s\b' % re.escape(s.strip()) for s in accounts]
there = re.compile('|'.join(patterns))
there2 = re.compile('|'.join(patterns2))
if there.search(tweet.text) and there2.search(str(tweet.user.id)):
print("New")
tweet.retweet()
def on_error(self, status):
print("Error detected")
tweets_listener = MyStreamListener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
stream.filter(follow=['account1','account2'])
Error
ValueError Traceback (most recent call last)
~\anaconda3\lib\http\client.py in _get_chunk_left(self)
554 try:
555 chunk_left = self._read_next_chunk_size()
556 except ValueError:
~\anaconda3\lib\http\client.py in _read_next_chunk_size(self)
521 try:
522 return int(line, 16)
523 except ValueError:
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
IncompleteRead Traceback (most recent call last)
I then get a couple of incomplete reads, the last line of the error is:
ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
This should now be handled in the development version of Tweepy on the master branch by automatically attempting to reconnect. The underlying connection error is likely due to falling too far behind in the stream when processing Tweets. For now, you can attempt to reduce the processing within on_status, handle the error by restarting the stream, wait for Tweepy v4.0, or use the development version of Tweepy.
For more context, see https://github.com/tweepy/tweepy/issues/237 and https://github.com/tweepy/tweepy/issues/448.
I am using IBM Watson for Sentiment Analysis and I get Forbidden Error 403. I contacted the IBM customer support mentioning the error and they asked me to check here.
import sys
import json
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_watson.natural_language_understanding_v1 import Features, SentimentOptions
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('API Key')
natural_language_understanding = NaturalLanguageUnderstandingV1(version='2019-07 12',authenticator=authenticator)
response = natural_language_understanding.analyze(text='This is really Awesome', features = Features(sentiment=SentimentOptions())).get_result()
print(response)
The Error received is given below:
ERROR:root:Forbidden Traceback (most recent call last):
File "C:\filepath\filepath2\filepath3\ibm_cloud_sdk_core\base_service.py", line 229, in send response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403
import tweepy
import json
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter_api = tweepy.API(auth)
# I made a dict of different countries and their WOE_ID...
PLACE_WOE_ID = country_id[country]
place_trends = twitter_api.trends.place(_id=PLACE_WOE_ID)
Everytime I run my code I am getting the following error. I checked other posts on stackoverflow regarding twitter api but I haven't found a solution yet.
Traceback (most recent call last):
File "C:/Users/user/Documents/twipgm.py", line 44, in <module>
place_trends = twitter_api.trends.place(_id=PLACE_WOE_ID)
AttributeError: 'API' object has no attribute 'trends'
There is no method like
place()
in tweepy documentation.
place_trends = twitter_api.trends_place(..)
Should solve your problem, I strongly suggest you to check this docs.
Note that Tweepy 4.0 version has renamed this method to .get_place_trends().
You can follow this tutorial to get some recent updates on Tweepy >4.0 use cases.
I'm trying to use Python to search the Facebook Graph for Pages. When I use the Graph API Explorer on the Facebook webpage and I enter:
search?q=aquafresh&type=page
I get the results I'm looking for. When I do the same in Python (after installing the PythonForFacebook module):
post = graph.get_object("search?q=aquafresh&type=post")
I get:
facebook.GraphAPIError: Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
I believe I'm properly identifying with the token, I'm using the same one as the webpage and it works on the webpage. I'm also able to do basic queries in Python (e.g., querying for "me" works fine)
I figured out a way to do this that doesn't involve the Facebook Graph API. Instead, use the Requests library:
import requests
token = "your_token"
query = "your_query"
requests.get("https://graph.facebook.com/search?access_token=" + token + "&q=" + query + "&type=page")
You can directly use search method of `GraphAPI
import facebook
TOKEN = "" # Your GraphAPI token here.
graph = facebook.GraphAPI(access_token=TOKEN, version="2.7")
posts = graph.search(q="aquafresh", type="post")
print posts['data']
Instead of:-
$ pip install facebook-sdk
Use this:-
pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk
What works for me is:
graph.request('search', {'q': 'aquafresh', 'type': 'page'})
That’s not exactly what you need, but when I try to search for posts (rather than pages) containing “aquafresh,” I get:
In [13]: graph.request('search', {'q': 'aquafresh', 'type': 'post'})
---------------------------------------------------------------------------
GraphAPIError Traceback (most recent call last)
<ipython-input-13-9aa008df54ba> in <module>()
----> 1 graph.request('search', {'q': 'aquafresh', 'type': 'post'})
/home/telofy/.buildout/eggs/facebook_sdk-0.4.0-py2.7.egg/facebook.pyc in request(self, path, args, post_args)
296 except urllib2.HTTPError, e:
297 response = _parse_json(e.read())
--> 298 raise GraphAPIError(response)
299 except TypeError:
300 # Timeout support for Python <2.6
GraphAPIError: (#11) Post search has been deprecated
(I wonder why deprecation doesn’t result in a mere warning.)
The request method seems to be missing from the documentation, but it’s documented in the code.
This should work:
import facebook
aToken = "your access tocken"
graph = facebook.GraphAPI(access_token=aToken, version="2.10")
data = graph.search(
q='aquafresh',
type='post'
)