我正在尝试使用tweepy使用推文位置而不是用户位置下载推文。目前,我可以使用用户位置下载推文,但即使geo_enabled
返回True,也无法获取推文位置。
例如,假设user_a
来自纽约,但他是来自加利福尼亚的推文。我想要用户位置,纽约和推文位置,加利福尼亚州。
代码:
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import pandas as pd
import json
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf8')
ckey = 'key'
csecret = 'secret'
atoken = 'token'
asecret = 'secret'
#csvfile = open('StreamSearch.csv','a')
#csvwriter = csv.writer(csvfile, delimiter = ',')
class StdOutListener(StreamListener):
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_data(self, data):
self.num_tweets += 1
if self.num_tweets < 5: #Remove the limit of no. of tweets to 5
print data
return True
else:
return False
def on_error(self, status):
print status
l = StdOutListener()
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
stream = Stream(auth, l)
stream.filter(locations = [80.10,12.90,80.33,13.24] ) #user location
输出
userLocation, userTimezone, Coordinates,GeoEnabled, Language, TweetPlace
London,UK Amsterdam FALSE en null
Aachen,Germany Berlin TRUE de null
Kewaunee Wi TRUE en null
Connecticut, Eastern Time (US & Canada) TRUE en null
TRUE en null
Lahore, City of Gardens London TRUE en null
NAU class of 2018. Arizona FALSE en null
FALSE en null
Pacific Time (US & Canada) FALSE en null
以上给出的输出是海量数据的清理版本。即使Geolocation
已启用,我也无法获取推文位置,也无法获取co-ordinates
。
答案 0 :(得分:7)
geo_enabled == True
的推文没有给出推文位置?根据this,如果地点或坐标为无,则表示用户不允许该推文获得许可。打开geo_enabled的用户仍然必须明确允许显示其确切位置。此外,documentation州:
geo_enabled:如果为true,表示用户已启用 地理标记其推文的可能性。当使用POST状态/更新时,此字段必须为当前用户附加地理数据。
如果您按位置过滤,则只会包含落在请求的边界框内的推文,用户的位置字段不会用于过滤推文。如果坐标和位置为空,则推文将不会通过过滤器。
#filter all tweets from san francisco
myStream.filter(location= [-122.75,36.8,-121.75,37.8])
您可以从过滤器中捕获推文,然后检查作者&#39;与您感兴趣的领域相匹配的位置。
class StdOutListener(StreamListener):
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_data(self, data):
#first check the location is not None
if status.author.location and 'New York' in status.author.location:
self.num_tweets += 1
print data
if self.num_tweets < 5: #Remove the limit of no. of tweets to 5
return True
else:
return False
def on_error(self, status):
print status
请记住,过滤器允许所有推文,只要它传递其中一个参数,因此如果您需要更严格,只需在def on_data(self, data)
中包含条件子句,就像我在(3)中为作者位置所做的那样。< / p>