[英]Pass Python keyword as an argument?
我目前正在开发使用Twitter Streaming API的Python脚本。
问题是,当我调用stream.user()
它会要求一个类似stream.user(with=followings)
的参数,但是Python将“ with”检测为关键字,并给了我一个语法错误。 有什么办法可以绕过这个?
更新:
这是Twitter文档: https : //dev.twitter.com/streaming/userstreams#Data_from_accounts_the_user_follows ,它说如果我想从用户关注的账户中获取推文,则需要使用“ with = followings”。
更新2:
这是代码:
from twython import Twython, TwythonStreamer
class Streamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
user = '@' + data['user']['screen_name']
tweet = ': ' + data['text']
def on_error(self, status_code, data):
print status_code
self.disconnect()
stream = Streamer('XXX',
'XXX',
'XXX',
'XXX')
stream.user(with=followings)
链接至: https : //dev.twitter.com/streaming/reference/get/user
def user(self, **params):
注意双星**
如果存在形式* identifier,则将其初始化为接收任何多余位置参数的元组,默认为空元组。 如果存在** identifier形式,则它将初始化为一个新字典,该字典将接收任何多余的关键字参数,默认为一个新的空字典。
尝试这个:
params = {'with' : 'followings'}
stream.user(**params)
“ with”确实是python中的关键字
>>> import keyword
>>> keyword.iskeyword('with')
True
它没有在您的问题中指定,但我认为您想听特定用户的所有推文。 在这种情况下,您可以按以下方式使用tweepy流:
l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
streamer.userstream(_with='followings')
你可以在这里阅读更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.