You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用rtweet包抓取Twitter状态的完整对话线程?

How to Scrape a Full Twitter Conversation Thread with rtweet

Hey there! I’ve worked extensively with rtweet for Twitter data collection, so let me walk you through a reliable way to pull an entire conversation thread—including nested replies.

Step 1: Set Up Your Environment

First, make sure you have the necessary packages installed and authenticated with Twitter’s API:

# Install packages if you haven't already
install.packages(c("rtweet", "dplyr", "purrr"))

# Load them into your session
library(rtweet)
library(dplyr)
library(purrr)

# Authenticate with Twitter (follow the prompts to log in via your browser)
auth_setup_default()

Step 2: Use a Recursive Function to Capture Nested Replies

The get_replies() function in rtweet only grabs direct replies to a single tweet. To get the full thread (replies to replies, and so on), we’ll use a recursive function to traverse each level of the conversation:

# Recursive function to fetch all levels of a conversation thread
get_full_conversation <- function(tweet_id, max_depth = 10) {
  # Stop recursion if we hit our depth limit
  if (max_depth <= 0) return(tibble())
  
  # Get direct replies to the current tweet
  direct_replies <- get_replies(tweet_id, n = Inf)
  
  # Fetch the original tweet's details to include in the thread
  original_tweet <- lookup_tweets(tweet_id) %>%
    select(user_id, screen_name, text, created_at, status_id, in_reply_to_status_id)
  
  # If no direct replies, return just the original tweet
  if (nrow(direct_replies) == 0) {
    return(original_tweet)
  }
  
  # Recursively fetch replies for each direct reply
  nested_replies <- map_dfr(direct_replies$status_id, ~get_full_conversation(.x, max_depth - 1))
  
  # Combine everything into one data frame
  bind_rows(original_tweet, direct_replies, nested_replies)
}

Step 3: Run the Function on Your Target Tweet

Replace the target_tweet_id with the ID of the tweet you want to scrape (you can find this in the tweet’s URL—it’s the long number at the end):

# Replace with your target tweet's status ID
target_tweet_id <- "1685123456789012345"

# Fetch the full thread
complete_thread <- get_full_conversation(target_tweet_id)

# Optional: Sort the thread by timestamp to follow the conversation flow
complete_thread <- complete_thread %>% arrange(created_at)

Key Notes & Troubleshooting

  • Rate Limits: Twitter’s API has rate limits for fetching replies. rtweet will automatically handle retries, but for very long threads, you might need to wait a few minutes between runs.
  • Private Accounts: If the original tweet is from a private account, you’ll need to be following that account to access its replies.
  • Max Depth: The max_depth parameter prevents infinite recursion (in case of circular replies, though rare). Adjust it based on how deep you expect the conversation to go.

If you run into issues with missing replies or rate limiting, feel free to share more details about your specific use case—I can help tweak the function further!

内容的提问来源于stack exchange,提问作者Shi Min Chua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:26:59