Open kitaiyork opened 4 years ago
Here's what I used to pull full body (plus URL, title, section name, and category name all in one CSV file). 8 lines from the bottom is what pulls in the body text.
This code is for python 3 (and will not work well with python 2 if you have Unicode text).
import requests
import csv
# Set the request parameters
# Change the base URL according to what information is desired.
base_url = 'https://yoursubdomain.zendesk.com/'
# Use Your Zendesk Support Sign-On Credentials
user = 'ZD Username'
pwd = 'ZD Password'
# Path of the outputted csv file
csv_file = 'localpath/filename.csv'
# Get section data
section_url = base_url + 'api/v2/help_center/en-us/sections'
sections_output = []
response = requests.get(section_url, auth = (user, pwd))
section_data = response.json()
def get_section_name_by_id(id):
for section in section_data['sections']:
if section['id']==id:
return section['name']
def get_section_parent_by_id(id):
for section in section_data['sections']:
if section['id']==id:
return section['category_id']
# Get category data
categories_url = base_url + 'api/v2/help_center/en-us/categories'
categories_output = []
response = requests.get(categories_url, auth = (user, pwd))
categories_data = response.json()
def get_category_by_section_id(id):
for category in categories_data['categories']:
if category['id']==get_section_parent_by_id(id):
return category['name']
# Loop through all pages of articles
url = base_url + 'api/v2/help_center/en-us/articles.json?sort_by=title&sort_order=asc'
output = []
with open(csv_file, 'w', encoding="utf-8", newline='') as fp:
header = ['URL','TITLE','CATEGORY','SUBCATEGORY','ARTICLE_BODY']
writer = csv.DictWriter(fp, fieldnames = header)
writer.writeheader()
while url:
response = requests.get(url, auth = (user, pwd))
data = response.json()
for article in data['articles']:
output.append(article['title'])
writer.writerow({
'URL': article['html_url'],
'TITLE': article['title'],
'CATEGORY': get_category_by_section_id(article['section_id']),
'SUBCATEGORY': get_section_name_by_id(article['section_id']),
'ARTICLE_BODY': article['body']
})
print(data['next_page'])
url = data['next_page']
# Print number of articles
print('Number of articles:', len(output))
Hi Scott, I find your work helpful, thanks for your time spent on the topic. I have some task on my head, as I have to export some articles from Zendesk in CSV I was able to follow your Python code and extract the articles in HTML, but what I need to extract the article body in the CSV as well. do you have some ideas? It is not so that I did not try on my own ... but it did not work.