简体   繁体   中英

Using Tweepy to sort tweets by location

Using tweepy we can search for tweets by locations and for multiple locations, such as:

stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41], track=terms)

However when I try to put all the tweets from NY in one list and tweets from SF in another list it, I cannot them to be assigned to either list. This is my code fragment:

NY = 74,40,-73,41
NewYorkCNN = []
if status.coordinates == NY or status.place == 'New York':
    for term in terms:
        if term in status.text:
            NewYorkCNN.append(term)

How could they be correctly placed in the correct list?

You've probably found out the answer by now, but it is something I struggled with too and after a while found out a simple enough solution.

Here is how I check the coordinates and work out whether to assign a tweets data into either a UK or a US database.

The "coordinates" keyword followed by the [1] or [2] gets the lat / long metadata from the tweet and compares it to the bounding assigned in the line below.

I hope this helps : )

class CustomStreamListener(tweepy.StreamListener):

  def check_ukcoords(self, status):
      if status.coordinates is not None:
          lat = status.coordinates['coordinates'][1]
          lng = status.coordinates['coordinates'][0]        
          return 49.7 < lat < 58.8 and -6.1 < lng < 1.7

  def check_uscoords(self, status):
      if status.coordinates is not None:
          lat = status.coordinates['coordinates'][1]
          lng = status.coordinates['coordinates'][0]        
          return 25.1 < lat < 49.1 and -125 < lng < -60.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM