【问题标题】:twitter full archive search pythontwitter 完整档案搜索 python
【发布时间】:2018-07-28 22:58:56
【问题描述】:

尝试使用 python 使用 twitter 的完整存档搜索。

基于此代码:http://benalexkeen.com/interacting-with-the-twitter-api-using-python/

我使用下面的代码没有成功

client_key = 'ZRNUXXXXXXXXXXXXXXXXXXXXXV0MTtQ'
client_secret = 'AypUFYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYAJww30xJI8'

import base64

key_secret = '{}:{}'.format(client_key, client_secret).encode('ascii')
b64_encoded_key = base64.b64encode(key_secret)
b64_encoded_key = b64_encoded_key.decode('ascii')

import requests

base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)

auth_headers = {
    'Authorization': 'Basic {}'.format(b64_encoded_key),
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}

auth_data = {
    'grant_type': 'client_credentials'
}

auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)

auth_resp.status_code

# Keys in data response are token_type (bearer) and access_token (your access token)
auth_resp.json().keys()

access_token = auth_resp.json()['access_token']

search_headers = {
    'Authorization': 'Bearer {}'.format(access_token)    
}

search_params = {
    'q': 'General Election',
    'result_type': 'recent',
    'count': 200
}

search_url = '{}1.1/search/Full-archive/Sandbox.json'.format(base_url) ## DOES NOT WORK
# ... search_url = '{}1.1/search/full-archive/Sandbox.json'.format(base_url) ## DOES NOT WORK
# ... search_url = '{}1.1/search/Fullarchive/Sandbox.json'.format(base_url) ## DOES NOT WORK
# ... search_url = '{}1.1/search/FullArchive/Sandbox.json'.format(base_url) ## DOES NOT WORK
# ... search_url = '{}1.1/search/fullarchive/Sandbox.json'.format(base_url) ## DOES NOT WORK

#search_url = '{}1.1/search/tweets.json'.format(base_url) ## <- THIS WORKS !! - standard api

search_resp = requests.get(search_url, headers=search_headers, params=search_params)

search_resp.status_code

tweet_data = search_resp.json()
# ... tweet_data

for x in tweet_data['statuses']:
    print(x['text'] + '\n')

使用标准搜索,没有问题。

对于我为完整档案搜索尝试的所有组合,search_resp.status_code 返回状态 404。

我的帐户拥有完整存档/沙盒环境。 我在创建环境时给出了开发环境标签:fullSearchSandbox。 (如果这很重要)。

如果有人可以在这里为我指出一个好的方向,谢谢。

【问题讨论】:

  • httpstatuses.com 状态 404 表示找不到页面。因此,您尝试访问的任何 URL 都不存在。
  • 谢谢@Joel。我花了几个小时在 twitter 社区和其他地方阅读我可以阅读的内容,以找到可能有效的正确组合。我希望其他人已经成功地解决了这个问题并可以帮助我。在上述尝试中,此 url 确实存在:search_url = '{}1.1/search/tweets.json'.format(base_url) 并访问标准 api 可用性。但是这个网址没有:search_url = '{}1.1/search/fullarchive/my_environment_sandbox.json'.format(base_url)。我正在寻找正确的声明内容和/或授权来完成这项工作。

标签: python twitter


【解决方案1】:

将 search_params 和 search_url 更新为以下内容:

search_params = {'query': 'TwitterDev'}  
search_url = '{}1.1/tweets/search/fullarchive/fullSearchSandbox.json'.format(base_url) 

这解决了问题...查询现在返回 status_code = 200 并填充了 tweets dict。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多