Sit*_*ogz 11 python twitter geolocation tweepy
我试图使用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
Run Code Online (Sandbox Code Playgroud)
产量
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
Run Code Online (Sandbox Code Playgroud)
上面给出的输出是海量数据的清理版本.即使Geolocation
已启用,我也无法获取推文位置,也无法获取co-ordinates
.
geo_enabled == True
没有给出推文位置?根据这个,如果地点或坐标是None,则表示用户不允许该推文的许可.打开geo_enabled的用户仍然必须明确允许显示其确切位置.此外,文档说明:
geo_enabled:如果为true,则表示用户已启用对其推文进行地理标记的 可能性.当使用POST状态/更新时,此字段必须为true才能使当前用户附加地理数据.
如果按位置过滤,则只会包含属于请求的边界框内的推文,用户的位置字段不会用于过滤推文.如果坐标和位置为空,则推文将不会通过过滤器.
#filter all tweets from san francisco
myStream.filter(location= [-122.75,36.8,-121.75,37.8])
Run Code Online (Sandbox Code Playgroud)
您可以从过滤器中捕获推文,然后检查作者的位置以匹配您感兴趣的区域.
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
Run Code Online (Sandbox Code Playgroud)
请记住,过滤器允许所有推文,只要它传递其中一个参数,因此如果您需要更严格的限制,只需def on_data(self, data)
在(3)中为作者位置包含条件子句.