Closed nuertey closed 4 years ago
To answer my own question, I was able to accomplish this with recursion. So I am closing this issue.
I am reopening this issue as I realize that even with the way that I am successfully recursively parsing the Google Trends Categories, the order in which the elements display do not seem quite correct. Either the sub-categories (or children) are displayed ahead of the major categories, or when reversed the whole list is displayed in the wrong alphabetical order (descending). Surely someone has tried to parse and view the Google Trends Categories before and has some insights to share?
So, anyone know how to correctly parse and display the Google Trends Categories list much like this: Google Trends Categories Thank you Nuertey
Hi I made a version that transforms the result of TrendReq().categories() in a string exactly as in the link Google Trends Categories If you need to put the subcategories in a dataframe you just have to replace the part 'text = text + ..' with something that appends to the dataframe the way you need
there's also some code to transform it into a json or to a html
import json
from json2html import *
from pytrends.request import TrendReq
categories = TrendReq().categories()
#If you want a file json from the categories:
json_categories = json.dumps(categories)
#if you want an html file from the categories (you need to specify classes):
html_categories = json2html.convert(json_categories)
#the text string generated from the categories:
text = '\u2022 ' + categories['name'] + ': ' + str(categories['id'])
cats = categories['children']
for i in range(len(cats)):
cat = cats[i]
text = text + '\n \u25CB ' + cat['name'] + ': ' + str(cat['id'])
scats = cat['children']
for i in range(len(scats)):
scat = scats[i]
text = text + '\n \u25AA ' + scat['name'] + ': ' + str(scat['id'])
if len(scat) > 2:
sscats = scat['children']
for i in range(len(sscats)):
sscat = sscats[i]
text = text + '\n \u25AA ' + sscat['name'] + ': ' + str(sscat['id'])
if len(sscat) > 2:
ssscats = sscat['children']
for i in range(len(ssscats)):
ssscat = ssscats[i]
text = text + '\n \u25AA ' + ssscat['name'] + ': ' + str(ssscat['id'])
if len(ssscat) > 2:
sssscats = ssscat['children']
for i in range(len(sssscats)):
sssscat = sssscats[i]
text = text + '\n \u25AA ' + sssscat['name'] + ': ' + str(sssscat['id'])
if len(sssscat) > 2:
ssssscats = sssscat['children']
for i in range(len(ssssscats)):
ssssscat = ssssscats[i]
text = text + '\n \u25AA ' + ssssscat['name'] + ': ' + str(ssssscat['id'])
Hi @tpilkati Excellent! This is precisely what I needed. Thank you a million. Nuertey
Anyone know how to properly and efficiently parse and print the Google Trends Categories listing, the result of executing TrendReq().categories()? It seems to be a dictionary of a list of a dictionary of a list and so on, nested several levels deep. Examples of the output are below.
I can get at the topmost level alright (as indicated in the second output below) but I want to learn the correct way (Pythonic?) to parse, print and use the complete list. Either parsing it into one complete dataframe of 'name' and 'id' columns and/or printing it similar to Google Trends Categories would do. Perhaps with list comprehensions? Recursion maybe? A combination thereof? Thank you Nuertey