scraping-tweets-from-account

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Scraping Tweets from an Account

抓取账号推文

Exports the full tweet history of any public Twitter/X account. Returns the raw timeline dataset including replies and media.
导出任意公开Twitter/X账号的完整推文历史。返回包含回复和媒体内容的原始时间线数据集。

Prerequisites

前置条件

  • APIFY_TOKEN
    environment variable set
  • Optional: Apify MCP server installed
  • 设置
    APIFY_TOKEN
    环境变量
  • 可选:安装Apify MCP服务器

Inputs

输入参数

ParameterTypeRequiredDefaultNotes
searchTerms
array
[]
Twitter advanced search queries (e.g.
["#AI lang:en", "from:NASA"]
)
sort
stringOptional
Top
Sort order:
Latest
,
Top
, or
Latest+Top
tweetLanguage
stringOptionalISO 639-1 language code (e.g.
en
)
maxItems
numberOptionalUnlimitedMaximum tweets to return
onlyVerifiedUsers
booleanOptional
false
Only tweets from verified users
onlyTwitterBlue
booleanOptional
false
Only Twitter Blue subscribers
onlyImage
booleanOptional
false
Only tweets with images
onlyVideo
booleanOptional
false
Only tweets with videos
onlyQuote
booleanOptional
false
Only quote tweets
author
stringOptionalFilter to a specific author handle
inReplyTo
stringOptionalTweets replying to a specific handle
mentioning
stringOptionalTweets mentioning a specific handle
geotaggedNear
stringOptionalTweets near a location
withinRadius
stringOptionalRadius around geotaggedNear
geocode
stringOptionalLat/lng + radius string
placeObjectId
stringOptionalTweets tagged with a place
minimumRetweets
numberOptionalMinimum retweet count
minimumFavorites
numberOptionalMinimum like count
minimumReplies
numberOptionalMinimum reply count
start
stringOptionalTweets after this date (YYYY-MM-DD)
end
stringOptionalTweets before this date (YYYY-MM-DD)
includeSearchTerms
booleanOptional
false
Add the matched search term to each tweet
customMapFunction
stringOptionalJavaScript function to transform each output object
参数类型是否必填默认值说明
searchTerms
数组
[]
Twitter高级搜索查询(例如
["#AI lang:en", "from:NASA"]
sort
字符串可选
Top
排序方式:
Latest
Top
Latest+Top
tweetLanguage
字符串可选ISO 639-1语言代码(例如
en
maxItems
数字可选无限制返回的最大推文数量
onlyVerifiedUsers
布尔值可选
false
仅返回认证用户的推文
onlyTwitterBlue
布尔值可选
false
仅返回Twitter Blue订阅者的推文
onlyImage
布尔值可选
false
仅返回包含图片的推文
onlyVideo
布尔值可选
false
仅返回包含视频的推文
onlyQuote
布尔值可选
false
仅返回引用推文
author
字符串可选过滤特定作者的账号
inReplyTo
字符串可选回复特定账号的推文
mentioning
字符串可选提及特定账号的推文
geotaggedNear
字符串可选特定地点附近的推文
withinRadius
字符串可选
geotaggedNear
的覆盖半径
geocode
字符串可选纬度/经度 + 半径字符串
placeObjectId
字符串可选标记特定地点的推文
minimumRetweets
数字可选最小转发数
minimumFavorites
数字可选最小点赞数
minimumReplies
数字可选最小回复数
start
字符串可选该日期之后的推文(格式:YYYY-MM-DD)
end
字符串可选该日期之前的推文(格式:YYYY-MM-DD)
includeSearchTerms
布尔值可选
false
为每条推文添加匹配的搜索关键词
customMapFunction
字符串可选用于转换每个输出对象的JavaScript函数

Workflow

工作流程

Progress:
- [ ] Step 1: Confirm account is public
- [ ] Step 2: Run tweet-scraper
- [ ] Step 3: Poll for SUCCEEDED
- [ ] Step 4: Fetch and deliver dataset
Progress:
- [ ] Step 1: Confirm account is public
- [ ] Step 2: Run tweet-scraper
- [ ] Step 3: Poll for SUCCEEDED
- [ ] Step 4: Fetch and deliver dataset

Step 1: Validate Input

步骤1:验证输入

Strip
@
from username if present. Do not attempt to scrape private accounts — the actor will return 0 results.
如果用户名包含
@
则去除。请勿尝试抓取私人账号——该Actor将返回0条结果。

Step 2: Run the Actor

步骤2:运行Actor

Recommended — run_actor.js (handles waiting, output, and file saving automatically):
bash
undefined
推荐方式——run_actor.js(自动处理等待、输出和文件保存):
bash
undefined

Quick answer (prints table to chat)

Quick answer (prints table to chat)

node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'
node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'

Save as CSV

Save as CSV

node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'
--output YYYY-MM-DD_results.csv --format csv
node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'
--output YYYY-MM-DD_results.csv --format csv

Save as JSON

Save as JSON

node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'
--output YYYY-MM-DD_results.json --format json
> `APIFY_TOKEN` must be set in environment or `.env` file.

**If Apify MCP is available:**
Tool: apify:run-actor Actor: "apidojo~tweet-scraper" Input: { "twitterHandles": ["<handle>"], "maxItems": 200, "sort": "Latest" }

**REST API fallback:**
```bash
curl -X POST \
  "https://api.apify.com/v2/acts/apidojo~tweet-scraper/runs?token=$APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"twitterHandles": ["<handle>"], "maxItems": 200, "sort": "Latest"}'
Save
id
as
RUN_ID
. Poll until
status = SUCCEEDED
:
bash
curl "https://api.apify.com/v2/actor-runs/$RUN_ID?token=$APIFY_TOKEN" | grep '"status"'
Fetch results:
bash
curl "https://api.apify.com/v2/actor-runs/$RUN_ID/dataset/items?token=$APIFY_TOKEN&format=json"
node scripts/run_actor.js
--actor "apidojo~tweet-scraper"
--input '{"param": "value"}'
--output YYYY-MM-DD_results.json --format json
> `APIFY_TOKEN`必须在环境变量或`.env`文件中配置。

**如果Apify MCP可用:**
Tool: apify:run-actor Actor: "apidojo~tweet-scraper" Input: { "twitterHandles": ["<handle>"], "maxItems": 200, "sort": "Latest" }

**REST API备选方案:**
```bash
curl -X POST \
  "https://api.apify.com/v2/acts/apidojo~tweet-scraper/runs?token=$APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"twitterHandles": ["<handle>"], "maxItems": 200, "sort": "Latest"}'
保存返回的
id
RUN_ID
,轮询直到
status = SUCCEEDED
:
bash
curl "https://api.apify.com/v2/actor-runs/$RUN_ID?token=$APIFY_TOKEN" | grep '"status"'
获取结果:
bash
curl "https://api.apify.com/v2/actor-runs/$RUN_ID/dataset/items?token=$APIFY_TOKEN&format=json"

Step 3: Handle Edge Cases

步骤3:处理边缘情况

  • 0 results: Account may be private, suspended, or handle misspelled. Report to user.
  • Fewer results than expected: Account may have fewer public tweets than requested. Return what is available.
  • Protected account: Actor returns empty — inform user the account is private.
  • 0条结果:账号可能是私人账号、已被封禁,或用户名拼写错误。请告知用户。
  • 结果数量少于预期:账号的公开推文数量可能少于请求的数量。返回现有可用内容即可。
  • 受保护账号:Actor将返回空结果——告知用户该账号为私人账号。

Output Format

输出格式

undefined
undefined

Tweet Timeline: @<username>

Tweet Timeline: @<username>

Tweets collected: N | Includes replies: YES/NO | Includes retweets: YES/NO
Tweet IDText (truncated)LikesRetweetsRepliesMediaTimestamp
.....................
Full dataset: N rows × 12 fields Available fields: id, text, likeCount, retweetCount, replyCount, quoteCount, isReply, isRetweet, media, tweetUrl, lang, createdAt
undefined
Tweets collected: N | Includes replies: YES/NO | Includes retweets: YES/NO
Tweet IDText (truncated)LikesRetweetsRepliesMediaTimestamp
.....................
Full dataset: N rows × 12 fields Available fields: id, text, likeCount, retweetCount, replyCount, quoteCount, isReply, isRetweet, media, tweetUrl, lang, createdAt
undefined

Troubleshooting

故障排除

0 results for a known public account: Try again — Twitter may rate-limit intermittently. Missing older tweets: Twitter API limits historical access; very old tweets may not be available. Timeout: Reduce
maxItems
to 500 max per run.
已知公开账号返回0条结果:重试即可——Twitter可能会间歇性触发速率限制。 缺少旧推文:Twitter API对历史数据访问有限制;非常早期的推文可能无法获取。 超时:将
maxItems
减少至每次运行最多500条。