简体   繁体   中英

Extracting tweets from a location based on profile in tweepy

I'm developing a piece of code whereby I want to track a key word using the Twitter streaming API.

Obviously, when I track the key word I get global results. I'm aware you cant stream both key words and locations simulatiously due to the API parameter requirements.

Instead I plan on only saving the streamed tweets which list their profile location as being in a certain city (London in this case).

Currently I'm trying the following

def on_status(self,status):
    try:
        locations=status.user.location
        if locations == ['London']:
           #Save to database
        else:
             pass

However, this isn't saving any tweets which contain the profile location as London

I think you should change this line:

if locations == ['London']: // status.user.location returns a string, not a list!

to:

if locations == 'London':

Your code didn't work because status.user.location returned a string and you compared this string with a list. That couldn't work.

And one other thing: since status.user.location can be "London, England" it's better to do some normalization and check on substring inclusion:

if 'london' in locations.lower():

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