Use api.search_comments() and api.search_submissions() to query the Pushshift API. These methods return generator objects, meaning they yield results one by one and support paging automatically.
Key features:
- Limit results: Use the
limit parameter to stop after a certain number of results. Omitting it performs a full historical search. - Filtering: Use the
filter parameter to return only specific fields (e.g., ['url', 'author']). - Time ranges: Use
after or before with Unix timestamps to scope searches. - Stop condition: Pass a lambda or function to the
stop_condition argument to stop yielding results based on custom logic (e.g., finding the first bot account).
# Get 100 most recent submissions
gen = api.search_submissions(limit=100)
results = list(gen)
# Search with specific filters and time range
import datetime as dt
start_epoch = int(dt.datetime(2017, 1, 1).timestamp())
results = list(api.search_submissions(
after=start_epoch,
subreddit='politics',
filter=['url', 'author', 'title', 'subreddit'],
limit=10
))
# Use a stop_condition
gen = api.search_submissions(stop_condition=lambda x: 'bot' in x.author)
for subm in gen:
pass