Twitter Streaming API过滤器-按位置过滤出现错误-对于按工作进行过滤正常工作

时间:2019-02-18 05:20:11

标签: python twitter tweepy

我正在使用Twitter API来获取流数据

for tweet in api.GetStreamFilter(locations=-122.75,36.8,-121.75,37.8):
    print (tweet)
    break

当我尝试按位置进行过滤时,我收到一条错误消息

File "<ipython-input-28-51193e42f674>", line 2
    for tweet in api.GetStreamFilter(locations=-122.75,36.8,-121.75,37.8):
                                                      ^
***SyntaxError: positional argument follows keyword argument***

如果我对单词使用过滤器

for tweet in api.GetStreamFilter(track = 'Facebook'):
    print (tweet)
    break

它正常工作。

当我使用位置信息时,我会收到错误消息。

我正在关注

https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters

并显示位置

Parameter value             Tracks Tweets from...
-122.75,36.8,-121.75,37.8   San Francisco
-74,40,-73,41               New York City
-122.75,36.8,-121.75,37.8,-74,40,-73,41 
                            San FranciscoOR New York City

更新

我遇到以下错误

enter image description here

TypeError                                 Traceback (most recent call last)
<ipython-input-37-bd71c23fee89> in <module>()
      1 
----> 2 for tweet in api.GetStreamFilter(locations=(-122.75,36.8,-121.75,37.8)):
      3     print (tweet)
      4     break

~\AppData\Local\Continuum\anaconda3\lib\site-packages\twitter\api.py in GetStreamFilter(self, follow, track, locations, languages, delimited, stall_warnings, filter_level)
   4580             data['track'] = ','.join(track)
   4581         if locations is not None:
-> 4582             data['locations'] = ','.join(locations)
   4583         if delimited is not None:
   4584             data['delimited'] = str(delimited)

TypeError: sequence item 0: expected str instance, float found

1 个答案:

答案 0 :(得分:0)

api.GetStreamFilter(locations=-122.75,36.8,-121.75,37.8)

在函数调用的上下文中,逗号的主要用途是分隔参数。

因此,您要使用四个参数来调用函数:locations=-122.7536.8-121.7537.8。这是一个错误,因为关键字参数必须在位置参数(即常规参数)之后

如果您打算传递一个元组,则将其括在括号中以将其视为单个参数:

api.GetStreamFilter(locations=(-122.75,36.8,-121.75,37.8))