我正在尝试通过推文进行抓取,但得到TweepError:Twitter错误响应:状态代码=400。相同的代码之前工作正常,但是现在引发了错误。
import json
import csv
import tweepy
import re
def search_for_hashtags(consumer_key, consumer_secret, access_token, access_token_secret, hashtag_phrase):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
fname = '_'.join(re.findall(r"#(\w+)", hashtag_phrase))
with open('%s.csv' % (fname), 'w', encoding = 'utf8') as file:
w = csv.writer(file)
w.writerow(['timestamp', 'tweet_text', 'username', 'all_hashtags', 'followers_count'])
for tweet in tweepy.Cursor(api.search, q=hashtag_phrase+' -filter:retweets', \
lang="en", tweet_mode='extended').items():
w.writerow([tweet.created_at, tweet.full_text.replace('\n',' '), tweet.user.screen_name, [e['text'] for e in tweet._json['entities']['hashtags']], tweet.user.followers_count])
consumer_key = input('Consumer Key ')
consumer_secret = input('Consumer Secret ')
access_token = input('Access Token ')
access_token_secret = input('Access Token Secret ')
hashtag_phrase = input('Hashtag Phrase ')
if __name__ == '__main__':
search_for_hashtags(consumer_key, consumer_secret, access_token, access_token_secret, hashtag_phrase)