-1
我正在为需要提取tweets的项目工作。我已经在python中使用过tweepy,因为我使用python不太舒服,所以使用了各种可用的在线信息来源。我遇到了两个我一直无法通过Google搜索解决的问题! 1)我想要大约1000存储在一个文件中。我想我可以使用同一个计数变量,但不知道在哪里以及如何使用它。基本上,一旦我收到1000条推文,如何终止程序? 2)打印到文件时,出现“File”Tweet3.py“,第20行,on_status print”Tweet Text:%s“%status.text UnicodeEncodeError:'ascii'codec can' t将字符u'\ u2019'编码到65位:序号不在范围内(128)“ - 我怎么能解决这个错误?使用tweepy在python中写入文件的问题
下面是代码:
import sys
import tweepy
import webbrowser
fp=open("Tweets.txt","w")
Q=['Earthquake','Flood']#Filters
c_key = '...'
c_secret = '...'
a_token= '...'
a_token_sec= '...'
auth = tweepy.OAuthHandler(c_key, c_secret)
auth.set_access_token(a_token, a_token_sec)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print "----------NEW TWEET!-----------"
print "Tweet Text : %s"%status.text
fp.write(status.text)
print "Author's name : %s"%status.author.screen_name
print "Time/Date of creation : %s"%status.created_at
print "Source of Tweet : %s"%status.source
print "Coordinates : %s"%status.coordinates
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
print "Displaying Tweets for filters :"
#print Q
#streaming_api.filter(follow=None, track=Q)
streaming_api.filter(locations=[-125,25,-65,48], async=False)
现在说,“UnicodeEncodeError: 'ascii'编解码器无法对位置40处的字符u'\ U0001f603'进行编码:序号不在范围内(128)“ – chappa
更新了我在Python中使用编解码器模块的答案。 – Rishi
非常感谢!这样做的工作! :) 有关第一个查询的任何帮助? – chappa