简体   繁体   中英

twitter full archive search python

Trying to use full archive search of twitter using python.

Based on this code : http://benalexkeen.com/interacting-with-the-twitter-api-using-python/

I am using the below code without success

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')

Using standard search, there is no problem.

With all combinations that I have tried for the full archive search, the search_resp.status_code returns status 404.

I have Full Archive / Sandbox environment with my account. I have given that dev env label : fullSearchSandbox when I created the environment. (if that matters).

Thanks if someone can point me in a good direction here.

Updated search_params and search_url to following :

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

This resolves the problem ... query now returns status_code = 200 and tweets dict is populated.

Thanks.

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