Pergunta

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

Foi útil?

Solução

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():
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top