Twitter Streaming API过滤器 - 按位置过滤获取错误 - 按工作过滤可正常工作

问题描述 投票:0回答:1

我正在使用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
python twitter tweepy
1个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.