Skip to content

Instantly share code, notes, and snippets.

@Suleman-Elahi
Last active March 23, 2021 07:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Suleman-Elahi/c3623c95182bd5ee3d653e1b896fd442 to your computer and use it in GitHub Desktop.
Save Suleman-Elahi/c3623c95182bd5ee3d653e1b896fd442 to your computer and use it in GitHub Desktop.
Get All WordPress Tags in Excel (CSV) using WordPress WP API v2
import requests
import csv
import time
from tqdm import tqdm
tags = {}
pages = int(requests.get('https://www.example.com/wp-json/wp/v2/tags').headers['X-WP-TotalPages'])
for i in tqdm(range(pages), ncols=65):
js = requests.get('https://www.example.com/wp-json/wp/v2/tags?page='+str(i+1)).json()
for entry in js:
tags[entry['name']]=entry['slug']
time.sleep(1)
with open("tags.csv", "w", newline='') as f:
datawriter = csv.writer(f)
datawriter.writerows(tags.items())
@Suleman-Elahi
Copy link
Author

Works like a charm:

image

Can also be used to get Posts (title, URLs) after a little tweaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment