I have set up my Python script. I find that for basic queries results are returned. However when I begin to put in some custom queries I get no results.

Let’s take for example, I am trying to get tweets that have the hashtag #Brexit that are in English and emanated from United Kingdom (GB) in the past 2 weeks. I then used this below for my gen_rule_payload but it gets me no results.

rule = gen_rule_payload(“#BREXIT place_country=gb lang=en”,
from_date=“2019-03-10”, to_date=“2019-03-24”, results_per_call=50)

Can someone help me out to see what I am doing wrong?

Oh, it might just be a minor thing, it’s lang:en and place_country:gb with colons instead of = sign.

Also, worth remembering that only a small percentage of tweets will have geo data. Have a look at the list of all parameters for more: Operators by product | Docs | Twitter Developer Platform

Hope that fixed it!

1 Like

Thanks. I got results now using the code below so I assume that that worked

rule = gen_rule_payload(“#BREXIT place_country:gb lang:en”,
from_date=“2019-03-10”, to_date=“2019-03-24”, results_per_call=50)

So generally if I want to use extra operators I just add it in the search string and separate it with " "?

Yep, one thing to remember when adding operators is: spaces imply AND, so if you search for “#brexit omnishambles” you’ll get tweets that contain both #brexit and “omnishambles”, if you want OR you have to specify: (#brexit OR omnishambles)

i’d also change results_per_call to the default 100 though, because setting it to 50 just makes you waste calls - if you’re going to spend a call, you may as well request the maximum available data.

Thanks for the 100 tip. As a follow up question to that, I believe I even asked earlier but now it becomes more pertinent as I am actually writing code. If I want to get the next 100 tweets ensuring that I don’t repeat and get any of my previous, what do you think will be the best approach?

The next parameter is used for pagination in Premium API Premium search APIs | Docs | Twitter Developer Platform

But if you’re using search-tweets-python, this is done for you for convenience: An example of it working is here: search-tweets-python/api_example.ipynb at master · twitterdev/search-tweets-python · GitHub

I get, so it handles it in the background. So if you request for 500, it actually calls the API 5 different times without you knowing.

Another question, the Twitter Parser object gets a good number of fields but it doesn’t get all e.g. place fields which have country, country_code, etc (I have put an example of what is returned below). How exactly can I access those fields as trying to access them using twitter.place.country is not working.

‘place’: {‘attributes’: {},
‘bounding_box’: {‘coordinates’: [[[-2.319934, 53.343623],
[-2.319934, 53.570282],
[-2.147026, 53.570282],
[-2.147026, 53.343623]]],
‘type’: ‘Polygon’},
‘country’: ‘United Kingdom’,
‘country_code’: ‘GB’,
‘full_name’: ‘Manchester, England’,
‘id’: ‘315b740b108481f6’,
‘name’: ‘Manchester’,
‘place_type’: ‘city’,
‘url’: ‘https://api.twitter.com/1.1/geo/id/315b740b108481f6.json’},

Oh, i think that’s easier to do if you don’t use Twitter Parser library, and extract that straight from the JSON object.

But it creates the object automatically and all the sample code uses tweet_parser. Do you have any alternate example?

Ah i see what you mean - the Tweet object in Tweet Parser is a dict though, so you should be able to get any original data out of that too - same was as any dict, tweet['property']

1 Like

Yep, that worked. I used the dictionary format to access the variables.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.