gpt4 book ai didi

python - Tweepy 位置过滤器不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:50 24 4
gpt4 key购买 nike

问题已解决,请参阅接受的帖子中的解决方案

我正在尝试收集来自特定地理区域的 50 条推文。我下面的代码将打印 50 条推文,但很多推文的坐标都为“NONE”。这是否意味着这些带有“NONE”的推文不是从指定区域生成的?你能解释一下这里发生了什么吗?以及如何从这个指定的地理区域收集 50 条推文?提前致谢。

# Import Tweepy, sys, sleep, credentials.py
try:
import json
except ImportError:
import simplejson as json
import tweepy, sys
from time import sleep
from credentials import *

# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# Assign coordinates to the variable
box = [-74.0,40.73,-73.0,41.73]

#override tweepy.StreamListener to add logic to on_status
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.counter = 0

def on_status(self, status):
record = {'Text': status.text, 'Coordinates': status.coordinates, 'Created At': status.created_at}
self.counter += 1
if self.counter <= 50:
print record
return True
else:
return False

def on_error(self, status_code):
if status_code == 420:
#returning False in on_data disconnects the stream
return False

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(api.auth, listener=myStreamListener)
myStream.filter(locations=box, async=True)
print myStream

结果如下:

{'Text': u"What?...", 'Created At': datetime.datetime(2017, 3, 12, 2, 55, 6), 'Coordinates': {u'type': u'Point', u'coordinates': [-74.
1234567, 40.1234567]}}
{'Text': u'WHEN?...', 'Created A
t': datetime.datetime(2017, 3, 12, 2, 55, 8), 'Coordinates': None}
{'Text': u'Wooo...', 'Created At': datetime.datetime(2017, 3, 12, 2, 55, 9), 'Coordinates': None}
{'Text': u'Man...', 'Created At': datetime.datetime(2017, 3, 12, 2, 55, 9), 'Coordina
tes': None}
{'Text': u'The...', 'Created At': datetime.datetime(201
7, 3, 12, 2, 55, 10), 'Coordinates': None}

最佳答案

来自文档:

Only geolocated Tweets falling within the requested bounding boxes will be included—unlike the Search API, the user’s location field is not used to filter Tweets.

这保证了响应中的推文来自提供的边界框。

边界框过滤器如何工作?

The streaming API uses the following heuristic to determine whether a given Tweet falls within a bounding box:

  • If the coordinates field is populated, the values there will be tested against the bounding box. Note that this field uses geoJSON order (longitude, latitude).

  • If coordinates is empty but place is populated,the region defined in place is checked for intersection against the locations bounding box. Any overlap will match. If none of the rules listed above match, the Tweet does not match the location query.

同样,这意味着坐标字段可以是 None 但 bbox 过滤器保证从边界框区域返回推文

来源:https://dev.twitter.com/streaming/overview/request-parameters#locations

编辑:place 是响应中类似于coordinates 的字段。

关于python - Tweepy 位置过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com