【问题标题】:Twitter Streaming API filter - Filter By Location Getting an error - For filter by work works correctlyTwitter Streaming API 过滤器 - 按位置过滤 出现错误 - 按工作过滤器正常工作
【发布时间】:2019-07-11 11:01:46
【问题描述】:

我正在使用 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】:

    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))
    

    【讨论】:

    • 用于 api.GetStreamFilter(locations = ['-122.75,36.8,-121.75,37.8']) 中的推文:打印(推文)中断 - 工作。谢谢约翰。
    猜你喜欢
    • 1970-01-01
    • 2013-09-15
    • 2011-05-22
    • 1970-01-01
    • 2019-11-12
    • 2023-02-20
    • 2015-02-14
    • 2013-08-16
    相关资源
    最近更新 更多