简体   繁体   中英

Getting twitter api to return search results instead of results summary

I'm using requests to grab tweets from a geographically bounded area. When I try to print the results, I get what looks like the summary of the results rather than the results themselves. My code is:

from __future__ import unicode_literals
import requests
from requests_oauthlib import OAuth1
import pprint


consumer_key=""
consumer_secret=""
access_key=""
access_token_secret=""
access_token=""
header_auth=True

url = 'https://api.twitter.com/1.1/search/tweets.json'

headeroauth = OAuth1(consumer_key, consumer_secret,
                     access_key, access_token_secret,
                     signature_type='auth_header')  

query_params = { 'q': 'the',
                 'geocode': '33.520661, -86.80249, 50mi' 
               }

response = requests.get(url, auth=headeroauth, params=query_params)
data = response.json()
pprint.pprint(data)

And the response I get back is:

{u'search_metadata': {u'count': 15, u'completed_in': 0.025000000000000001,
u'max_id_str': u'349363977614143489', u'since_id_str': u'0', u'refresh_url': u'
since_id=349363977614143489&q=the&geocode=33.520661%2C%20
86.80249%2C%2050mi&include_entities=1', u'since_id': 0, u'query': u'the', u'max_id': 349363977614143489}, u'statuses': []}

How do I set it up to return the actual content of the tweets? Thanks!

In twitter API's documentation when you use "search", you got this dict object and in statuses key you should have a list of tweets according to your search options. Be sure that tweets in statuses are dict objects too, and its content are in text key

Remove the spaces in your geocode parameter and it should return statuses:

query_params = { 'q': 'the',
                 'geocode': '33.520661,-86.80249,50mi' 
               }

Also check out the Twitter dev console for testing out your queries.

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