簡體   English   中英

無法使用Tweepy下載搜索到的推文

[英]Can't download searched tweets using Tweepy

我以前使用過以下腳本,但現在無法使用。 我沒有在終端中打印任何推文(如第38行中的代碼所示),也沒有在我的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!"

問題出在這一行:

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

您所做的就是創建一個包含列表的列表。 看下面的代碼:

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

print(searchTerms)
>>> ['#Xfactor', '#Yfactor']
print(type(q))
>>> [['#Xfactor', '#Yfactor']]

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

因此,現在您感興趣的tweet 必須包含文字['#Xfactor', '#Yfactor'] 要解決該問題,請將q更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM