【发布时间】: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('&','').replace('>','').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