【问题标题】:Tweepy Python Tuple Error no attribute encodeTweepy Python元组错误没有属性编码
【发布时间】:2014-07-14 14:21:24
【问题描述】:

我正在使用 tweepy 和 python 根据某些关键字收集推文,然后将这些状态更新(推文)写入 CSV 文件。我不认为自己是程序员,对此我真的很迷茫。

这是错误:

> Traceback (most recent call last):
  File "./combined-tweepy.py", line 58, in <module>
    sapi.filter(track=[topics])
  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 286, in filter
    encoded_track = [s.encode(encoding) for s in track]
AttributeError: 'tuple' object has no attribute 'encode'

这是脚本:

#!/usr/bin/python
import sys
import re
import tweepy
import codecs
import datetime

consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

# Create a list of topics
with open('termList.txt', 'r') as f:
  topics = [line.strip() for line in f]

stamp = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')
topicFile = open(stamp + '.csv', 'w+')
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(topicFile))
sapi.filter(track=[topics])

class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, output_file, api=None):
        super(CustomStreamListener, self).__init__()
        self.num_tweets = 0
        self.output_file = output_file

    def on_status(self, status):
        ### Writes one tweet per line in the CSV file
        cleaned = status.text.replace('\'','').replace('&amp;','').replace('&gt;','').replace(',','').replace("\n",'')
        self.num_tweets = self.num_tweets + 1
        if self.num_tweets < 500:
            self.output_file.write(status.user.location.encode("UTF-8") + ',' + cleaned.encode("UTF-8") + "\n")
            print ("capturing tweet from list")
            # print status.user.location
            return True
        else:
            return False
            sys.exit("terminating")

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True #Don't kill the stream

f.close()

【问题讨论】:

  • 'termList.txt' 里面有没有叫做 encode 的东西?
  • 我不知道如何在评论中以列表格式将其放入:BlackStone ViceLords Piru Crips Barrio Azteca FBD 624 BDS MLD Nortenos Tangos Vallucos Orejas Foritos Houstone Surenos Trinitarios Armanian Assyrian Nuestra Syndicate Hammerskins Lowriders Volksfront Capirucha Corpitos Tangos Mandingo Pocos Tongs Salvatrucha MS-13 Sureno 915 主题之一是 915 和 624,区号代表一个帮派。

标签: python twitter tuples tweepy twitter-streaming-api


【解决方案1】:

根据 Python 的文档,这是definition of a tupletopics 中的一个词似乎是一个元组。

我看到了其他小错误。首先,按照您编写代码的方式,您应该在定义函数后调用它们。比如这两行

sapi = tweepy.streaming.Stream(auth, CustomStreamListener(topicFile))
sapi.filter(track=[topics])

应该在你定义完所有的函数之后出现

class CustomStreamListener(tweepy.StreamListener):

另外,没有必要把topics放在大括号里

sapi.filter(track=[topics])

因为根据这一行已经是一个列表

topics = [line.strip() for line in f]

能给我们看看 termList.txt 的内容吗?

【讨论】:

  • 我把这个放在上面的评论里,txt文件每行一个条目。
  • 我认为您的主题列表的构建方式没有问题。尝试纠正我之前在您的代码中发现的小错误。也许,它会有所帮助。目前,我真的不知道还能做什么。
  • 谢谢 blue_chip 我会看看这会把我引向何方。
猜你喜欢
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
相关资源
最近更新 更多