无法使用Tweepy下载搜索到的tweets

2024-09-28 01:29:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前用过下面的脚本,但现在不行了。我没有看到任何tweets被打印在我的终端上(如第38行中的编码),也没有任何tweet被存储在我的csv中。我不知道是什么问题。

import tweepy
import csv
import time

access_token = "xxxxxxxxxx"
access_token_secret = "xxxxxxxxxx"
consumer_key = "xxxxxxxxxx"
consumer_secret = "xxxxxxxxxx"


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

print "Starting search..."

#
# Open/Create a file to append data
csvFile = open('somesearch.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)


searchTerms = ["#Xfactor", "#Yfactor"]


tweets= tweepy.Cursor(api.search,q=[searchTerms], \
                      since="2015-10-18",
                      until="2015-10-23",
                      include_entities=True).items(999999999)

#csvWriter.writerow([tweet.created_at, tweet.id_str, tweet.screen_name, tweet.user_id, tweet.coordinates, tweet.place, tweet.text.encode('utf-8'), tweet.retweet_count, tweet.favorite_count])
#tweet.in_reply_to_user_id_str, tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id_str, tweet.retweeted, tweet.truncated, tweet.source

while True:
    try:
        for tweet in tweets:
            print tweet.created_at, tweet.text.encode('utf-8')
            csvWriter.writerow([tweet.created_at, tweet.id_str, tweet.author.name.encode('utf-8'), tweet.author.screen_name.encode('utf-8'),
                                tweet.user.location.encode('utf-8'), tweet.coordinates, tweet.text.encode('utf-8'), tweet.retweet_count, tweet.favorite_count])
    except tweepy.TweepError:
        time.sleep(60 * 15)
        continue
    except StopIteration:
        break

print "Done!"

Tags: csvtonametokenidsecretaccessconsumer
1条回答
网友
1楼 · 发布于 2024-09-28 01:29:30

问题在于这一行:

tweets= tweepy.Cursor(api.search,q=[searchTerms],

您所做的就是创建一个包含列表的列表。看看这个代码:

^{pr2}$

您要搜索的不是searchTerms,而是要搜索的列表。在

所以现在你感兴趣的tweet必须包含文本['#Xfactor', '#Yfactor']。要解决该问题,请将q更改为:

tweets= tweepy.Cursor(api.search,q=searchTerms,

相关问题 更多 >

    热门问题