我如何为tweepy脚本修复此语法错误?

时间:2017-07-25 22:51:03

标签: python-2.7 twitter tweepy bigdata

您好我正在尝试学习项目的编程。

我一直在研究一个简单的脚本(使用tweepy)从搜索关键字到.csv格式下载推文。然而,我一直在多行(从28到38)得到一个sintax错误,我不知道此时有什么问题,有人可以告诉我什么是错的吗?

这是我一直在努力的代码......

    # -*- coding: utf-8 -*-
#import modules
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import unicodecsv
from unidecode import unidecode
import csv

from textblob import TextBlob

ckey = "XXXXXXXXXXXXXXXXXX"
csecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
atoken = "XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXX"
asecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret, 'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)
fName= raw_input("Nombre del Archivo: ")+'.csv'

for tweet in tweepy.Cursor(api.search, q=('dulceveneno'), since='2014-09-16', until='2017-07-25').items(5):

    tweet_info = [tweet.author.name.encode('utf8')
                tweet.author.screen_name.encode('utf8')
                tweet.created_at
                tweet.text.encode('utf8')
                tweet.retweeted
                tweet.favorited
                tweet.user.location.encode('utf8')
                tweet.user.time_zone
                tweet.geo
                tweet.entities.get('hashtags')]

    with open(fName, 'wb') as file:
        writer = unicodecsv.writer(file, delimiter = ',', quotechar = '"')
       # Write header row.
        writer.writerow(["Nombre",
                         "UserName",
                         "Fecha",
                         "Tweet",
                         "Retweet?"
                         "Favs"
                         "Ubicación",
                         "Horario",
                         "Geo",
                         "Hashtags"])


            # Write data to CSV.
            writer.writerow(tweet_info)


        # Show progress.
        print("DONE!" % q)

问题出在" tweet_info"部分我猜...

1 个答案:

答案 0 :(得分:0)

您尚未发布实际错误,但我可以看到tweet_info您的列表中的元素没有任何逗号,

应该是:

tweet_info = [tweet.author.name.encode('utf8'),
            tweet.author.screen_name.encode('utf8'),
            tweet.created_at,
            tweet.text.encode('utf8'),
            tweet.retweeted,
            tweet.favorited,
            tweet.user.location.encode('utf8'),
            tweet.user.time_zone,
            tweet.geo,
            tweet.entities.get('hashtags')]