2015-12-03 34 views
10

我試圖使用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

這個非常廣泛的問題,讓我們知道你已經嘗試過。 – SIslam

+0

@SIslam包括代碼和輸出 –

+1

可能是相關的 - http://stackoverflow.com/a/16892093/4065350 –

回答

6
  1. 爲什麼推文geo_enabled == True不給推文位置?

根據this,如果位置或座標爲無,則表示用戶不允許該推文的許可。開啓geo_enabled的用戶仍然必須明確授予其顯示的確切位置。此外,documentation指出:

geo_enabled:如果爲true,表明用戶已啓用地理標記的鳴叫的 可能性。當使用POST狀態/更新時,此字段必須爲當前用戶附加地理數據。

  1. 如何按推文位置過濾? Check here

如果按位置過濾,只落在要求邊框內推文將包括在內,用戶的位置字段不是用來過濾鳴叫。如果座標和位置爲空,則推文將不會通過該過濾器。

#filter all tweets from san francisco 
myStream.filter(location= [-122.75,36.8,-121.75,37.8]) 
  • 如何通過用戶位置和鳴叫的位置來過濾?
  • 您可以捕獲過濾器中的推文,然後檢查作者的位置以匹配您感興趣的區域。

    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 
    
  • 如何不限制自己到Twitter API過濾器?
  • 記住過濾允許所有的鳴叫,只要它傳遞的參數之一,所以如果你需要更嚴格的只包含條件子句中def on_data(self, data)爲I(3)筆者位置確實英寸