我尝试使用他们的API和Python 3.6从Twitter抓取数据,我想将结果保存在文本文件中以将其显示为条形图。
class listener (StreamListener):
def on_data(self, data):
print (data)
savefile=open("C:\\Users\Ahmed\Desktop\twitter\twitter.txt","a")
savefile.write(data)
savefile.write("\n")
savefile.close()
return True
def on_error(self,status):
print (status)
它给出了这个错误:
OSError:[Errno 22]参数无效:'C:\ Users \ Ahmed \ Desktop \ twitter \ twitter.txt'
我该如何解决?
答案 0 :(得分:0)
使用原始字符串:C:\Users\Ahmed\Desktop\twitter\twitter.txt
或加倍反斜杠。
通常,在您registered your app,执行以下操作后:
pip install tweepy==3.3.0
然后授权:
import tweepy
from tweepy import OAuthHandler
consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
阅读您自己的时间表:
for status in tweepy.Cursor(api.home_timeline).items(10):
# Process a single status
print(status.text)
粉丝列表:
for friend in tweepy.Cursor(api.friends).items():
process_or_store(friend._json)
推文列表:
for tweet in tweepy.Cursor(api.user_timeline).items():
process_or_store(tweet._json)