【问题标题】:Unable to get User Fields from Twitter Full Archive API无法从 Twitter Full Archive API 获取用户字段
【发布时间】:2021-06-25 17:37:00
【问题描述】:
import requests
import pandas as pd
import json
import time

def search_endpoint_connect(bearer_token, query, st, et, next_token):

headers = {"Authorization": "Bearer {}".format(bearer_token)}
query_params = {
                'query': query,
                'start_time': st,
                'end_time': et,
                'max_results': 100,
                'tweet.fields': 'id,text,author_id,created_at,geo,lang,public_metrics,in_reply_to_user_id,referenced_tweets', 
                'user.fields':'created_at,location,profile_image_url',
               }

if (next_token is not None):
    url = "https://api.twitter.com/2/tweets/search/all?next_token={}".format(next_token)
else:
    url = "https://api.twitter.com/2/tweets/search/all"





response = requests.request("GET", url, params=query_params, headers=headers)

if response.status_code != 200:
    raise Exception(response.status_code, response.text)

return response.json()

def main(bearer_token, n, fn, sq, st, et):

rl_count = 0
count = 0
flag = True
first = True


while flag:
    
    if rl_count==300:
        time.sleep(600)
        print('Rate limit cooldown 10 mins.')

    if count >= n and n!=0:
        break
    if not first:
        json_response = search_endpoint_connect(bearer_token, sq, st, et, next_token)
    else:
        json_response = search_endpoint_connect(bearer_token, sq, st, et, next_token=None)
    
    result_count = json_response['meta']['result_count']
    if 'next_token' in json_response['meta']:
        next_token = json_response['meta']['next_token']
    
        if result_count is not None and result_count > 0 and next_token is not None:
            print(json_response)
            df = pd.json_normalize(json_response['data'])
            df = df.reindex(columns=['id','text',
                                     'public_metrics.retweet_count',
                                     'public_metrics.favourite_count',
                                     'created_at', 'user.id','lang',
                                     'public_metrics.reply_count', 'public_metrics.like_count',
                                     'location',
                                     'in_reply_to_user_id','authorid.username',
                                     'place', 'geo.place_id', 
                                     'geo.coordinates.type', 
                                     'geo.coordinates.coordinates','referenced_tweets','referenced_tweets.id'])
            if not first:
                df.to_csv('%s.csv'%fn, mode='a', encoding='utf-8', index=False, header=None)
            else:
                df.to_csv('%s.csv'%fn, encoding='utf-8', index=False)

            time.sleep(1)
            count += result_count
            print('Tweets downloaded: '+str(count))
    else:
        flag = False
    rl_count += 1
    first = False


#Enter your bearer token
bearer_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

#Set number of tweets to be downloaded. Enter 0 for no limits
no_of_tweets = 20

#Specify the name of the output csv file. Do not include .csv
file_name = 'downloaded_tweets6'

#Enter your search query. Refer to https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-query
search_query = '(travel OR goa) lang:en point_radius:[-74.014746 40.730610  20mi]'

#point_radius:[23.8.050 -80.180374 16mi]

#Set the beginning date and time in YYYY-MM-DDTHH:MM:SSZ format
start_time = "2019-11-27T00:00:00Z"

#Set the ending date and time in YYYY-MM-DDTHH:MM:SSZ format
end_time = "2019-11-29T00:00:00Z"

#point_radius= "-41.287336,174.761070,20mi"

main(bearer_token, no_of_tweets, file_name, search_query, start_time, end_time)

我无法获取推文的用户信息(即用户名和用户位置)。相应的文档可以在以下链接中找到:“https://developer.twitter.com/en/docs/twitter-api/expansions”和“https://developer.twitter.com/en/docs/twitter-api /tweets/search/api-reference/get-tweets-search-all" 请帮忙 提前致谢

【问题讨论】:

    标签: python api web-scraping twitterapi-python


    【解决方案1】:

    我有同样的问题,我解决了它添加到 query_params 'expansions': 'author_id' (返回代表推文作者 https://developer.twitter.com/en/docs/twitter-api/expansions 的用户对象)并删除 'user.fields'。

    例如:

    query_params = {
                    'query': query,
                    'start_time': st,
                    'end_time': et,
                    'max_results': 100,
                    'expansions': 'author_id,referenced_tweets.id',
                    'tweet.fields': 'id,text,author_id,created_at,geo,lang,public_metrics,in_reply_to_user_id,referenced_tweets'
                   }
    

    然后你会得到一个带有键的 json_response 字典:'data'、'includes' 和 'meta':

    json_response.keys()
    Out[281]: dict_keys(['data', 'includes', 'errors', 'meta'])
    

    访问用户数据:

    json_response['includes']['users'] 
    

    返回一个字典列表,例如 {'id': 'xxxx','name': 'xxx','username': 'xxxx'}

    如果您需要更多用户字段,请参阅第 12 节。使用来自https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/6a-labs-code-academic-python.md 的推文 ID 查找推文,其中实现了从 id users 查找用户字段的功能。

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 2011-06-03
      • 2017-01-22
      相关资源
      最近更新 更多