Reputation: 125
I want to scrape tweets using API of free developer account. I do not have too much knowledge of using API for scraping.
The code that I am using for scraping is:
import tweepy
client = tweepy.Client(
consumer_key='aaa',
consumer_secret= 'aaa',
access_token='bbb',
access_token_secret='bbb'
)
client = tweepy.Client(consumer_key= consumer_key,consumer_secret= consumer_secret,access_token= access_token,access_token_secret= access_token_secret)
query = 'news'
tweets = client.search_recent_tweets(query=query, max_results=10)
for tweet in tweets.data:
print(tweet.text)
Unauthorized Traceback (most recent call last)
<ipython-input-27-cdd309f625b4> in <cell line: 12>()
10 client = tweepy.Client(consumer_key= consumer_key,consumer_secret= consumer_secret,access_token= access_token,access_token_secret= access_token_secret)
11 query = 'news'
---> 12 tweets = client.search_recent_tweets(query=query, max_results=10)
13 for tweet in tweets.data:
14 print(tweet.text)
2 frames
/usr/local/lib/python3.10/dist-packages/tweepy/client.py in request(self, method, route, params, json, user_auth)
96 raise BadRequest(response)
97 if response.status_code == 401:
---> 98 raise Unauthorized(response)
99 if response.status_code == 403:
100 raise Forbidden(response)
Unauthorized: 401 Unauthorized
Unauthorized
I tried to update the status of my API using the below code:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status("Hello Tweepy")
After using the above code for updating status I again run the tweet search code for scrapping but when I run the code than I get an error message. I do not know why this is happening. My developer app has read and write permissions generated keys.
Can anyone guide me how to remove this error and scrape the tweets?
Upvotes: 0
Views: 1324
Reputation: 516
Sorry, but you cannot scrape tweets ( Search Tweets ) using the Free Level Access on the Twitter API.
You can use the Manage Tweets and User Lookup features on Free Level Access.
For the Search Tweets feature, you have to upgrade to the Basic Level.
The syntax to call the Twitter API v2 is:
# Authenticate to Twitter
client = tweepy.Client(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_TOKEN,
access_token_secret=ACCESS_TOKEN_SECRET
)
# Post Tweet
message = "Hello Twitter"
client.create_tweet(text=message)
# Search Recent Tweets (This requires Basic Level Access)
query = 'QUERY'
tweets = client.search_recent_tweets(query=query, max_results=10)
for tweet in tweets.data:
print(tweet.text)
In the second code snippet, you are trying to call the Twitter API v1.1, which can only be used for Media Uploads.
The correct syntax to call the Twitter API v1.1 for Media Upload is:
# Authenticate to Twitter
auth = tweepy.OAuth1UserHandler(
CONSUMER_KEY, CONSUMER_SECRET,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET
)
# Create API object
api = tweepy.API(auth)
# Upload Image
media = api.media_upload("tweet_img.png")
For more information:
https://developer.twitter.com/en/docs/twitter-api
https://docs.tweepy.org/en/v4.14.0/client.html#tweepy.Client.search_recent_tweets
I hope this helps.
Upvotes: 1