Skip to main content
2 of 2
Reworked into a question and associated answer.
Nathan Osman
  • 23.3k
  • 11
  • 61
  • 107

Question: How do you request all Tags?

Retrieving a list of tags on a given Stack Exchange site with Stack.PY is relatively straightforward:

from stackpy import Site

sa = Site('stackapps')
for t in sa.tags:
    print t

However, this will only fetch the first 30 tags. The tricky part is fetching more than one page. You can fetch any particular page by sticking .page(n) on the end of the request chain (where n is the page to fetch).

If we rewrite the loop a bit, we end up with:

from itertools import count
from stackpy import Site

sa = Site('stackapps')
for p in count(1):
    tags = sa.tags.page(p)
    for t in tags:
        print t
    if not tags['has_more']:
        break

The example above uses a generator that will yield an infinite list of consecutive page numbers to fetch all of the pages. When the has_more property is set to false in the JSON returned by the API, the loop will terminate.

Eugene
  • 129
  • 2