【问题标题】:Getting twitter api to return search results instead of results summary让 twitter api 返回搜索结果而不是结果摘要
【发布时间】:2013-06-29 07:06:47
【问题描述】:

我正在使用请求从地理边界区域获取推文。当我尝试打印结果时,我得到的是结果摘要,而不是结果本身。我的代码是:

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)

我得到的回应是:

{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': []}

如何设置它以返回推文的实际内容?谢谢!

【问题讨论】:

    标签: python api twitter


    【解决方案1】:

    twitter API's documentation 中,当你使用“搜索”时,你会得到这个dict 对象,在statuses 键中,你应该有一个根据你的搜索选项的推文列表。确保 statuses 中的 tweet 也是 dict 对象,其内容在 text 键中

    【讨论】:

    • 我将如何确保状态中的推文存在并设置为 dict 对象?谢谢!
    【解决方案2】:

    删除地理编码参数中的空格,它应该返回状态:

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

    还可以查看 the Twitter dev console 以测试您的查询。

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多