簡體   English   中英

Tweepy Python元組錯誤無屬性編碼

[英]Tweepy Python Tuple Error no attribute encode

我正在使用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()

這是根據Python文檔的元組定義 似乎主題中的單詞之一是元組。

我看到其他小錯誤。 首先,編寫代碼的方式應在定義函數后調用它們。 例如,這兩行

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

應該在您定義了所有功能之后

class CustomStreamListener(tweepy.StreamListener):

此外,也無需將主題放在大括號中

sapi.filter(track=[topics])

因為它已經是根據此行的列表

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

您能告訴我們termList.txt的內容嗎?

暫無
暫無

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

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