#!/usr/bin/python3

"""
    get_allcategories.py

    MediaWiki Action API Code Samples
    Demo of Allcategories module: GET request to list all categories
    on the English Wikipedia, starting from "15th-century caliphs".
    MIT license
"""
import json

import pandas
import requests


wikipedia_url = "http://en.wikipedia.org/w/api.php"
# http://en.wikipedia.org/w/api.php?
# action=query&list=allcategories&acprop=size&acprefix=hollywood&format=json"

wikipedia_params = {"action": "query",
                    "list": "allcategories",
                    "acprop": "size",
                    "acprefix": "hollywood",
                    "format": "json"}

res = requests.get(url=wikipedia_url, params=wikipedia_params)
data = res.json()

allcategories = data.get("query", {}).get("allcategories")

# Переименовываем поле "*"
for categories in allcategories:
    categories["name"] = categories.get("*")
    categories.pop("*")

# Конвертируем данные в json
allcategories_as_json = json.dumps(allcategories)

# Конвертируем json d специальный формат пандаса - датафрейм
pandas_dataframe = pandas.read_json(allcategories_as_json)

# Cохраняем датафрейм как ексель
pandas_dataframe.to_excel("example.xlsx")


d958efdfd2ffb168c33b18be170fb03e2809837d
<mask token>

<mask token>
for categories in allcategories:
    categories['name'] = categories.get('*')
    categories.pop('*')
<mask token>
pandas_dataframe.to_excel('example.xlsx')

<mask token>
wikipedia_url = 'http://en.wikipedia.org/w/api.php'
wikipedia_params = {'action': 'query', 'list': 'allcategories', 'acprop':
    'size', 'acprefix': 'hollywood', 'format': 'json'}
res = requests.get(url=wikipedia_url, params=wikipedia_params)
data = res.json()
allcategories = data.get('query', {}).get('allcategories')
for categories in allcategories:
    categories['name'] = categories.get('*')
    categories.pop('*')
allcategories_as_json = json.dumps(allcategories)
pandas_dataframe = pandas.read_json(allcategories_as_json)
pandas_dataframe.to_excel('example.xlsx')

<mask token>
import json
import pandas
import requests
wikipedia_url = 'http://en.wikipedia.org/w/api.php'
wikipedia_params = {'action': 'query', 'list': 'allcategories', 'acprop':
    'size', 'acprefix': 'hollywood', 'format': 'json'}
res = requests.get(url=wikipedia_url, params=wikipedia_params)
data = res.json()
allcategories = data.get('query', {}).get('allcategories')
for categories in allcategories:
    categories['name'] = categories.get('*')
    categories.pop('*')
allcategories_as_json = json.dumps(allcategories)
pandas_dataframe = pandas.read_json(allcategories_as_json)
pandas_dataframe.to_excel('example.xlsx')

#!/usr/bin/python3

"""
    get_allcategories.py

    MediaWiki Action API Code Samples
    Demo of Allcategories module: GET request to list all categories
    on the English Wikipedia, starting from "15th-century caliphs".
    MIT license
"""
import json

import pandas
import requests


wikipedia_url = "http://en.wikipedia.org/w/api.php"
# http://en.wikipedia.org/w/api.php?
# action=query&list=allcategories&acprop=size&acprefix=hollywood&format=json"

wikipedia_params = {"action": "query",
                    "list": "allcategories",
                    "acprop": "size",
                    "acprefix": "hollywood",
                    "format": "json"}

res = requests.get(url=wikipedia_url, params=wikipedia_params)
data = res.json()

allcategories = data.get("query", {}).get("allcategories")

# Переименовываем поле "*"
for categories in allcategories:
    categories["name"] = categories.get("*")
    categories.pop("*")

# Конвертируем данные в json
allcategories_as_json = json.dumps(allcategories)

# Конвертируем json d специальный формат пандаса - датафрейм
pandas_dataframe = pandas.read_json(allcategories_as_json)

# Cохраняем датафрейм как ексель
pandas_dataframe.to_excel("example.xlsx")

