P
P
PeteDoherty2021-09-27 23:04:13
Python
PeteDoherty, 2021-09-27 23:04:13

Why does an error pop up when outputting from a dictionary with data?

Hello. I contacted the Head Hunter API to display data on vacancies. When two values ​​are input, for example (vacancies in Python, Java), the script works, but if more than two values ​​are input in the traceback:

TypeError: 'NoneType' object is not subscriptable


Tell me what's wrong?

import requests
import pprint
import bumpy


def search_vacations(language, url, page=None):
    payload = {
        'text': f'Программист {language}',
        'area': 1,
        'period': 30,
        'only_with_salary': 'True',
        'page': page
    }
    response = requests.get(url, params=payload)
    response.raise_for_status()
    return response.json()


def predict_rub_salary(language, url):
    salaries_bracket = get_salaries_bracket(language, url)
    predictioned_salaries = []
    for salary in salaries_bracket:
        if salary['currency'] != 'RUR':
            None
        elif salary['from'] and salary['to']:
            predictioned_salaries.append(numpy.mean([salary['from'], salary['to']]))
        elif salary['from']:
            predictioned_salaries.append(salary['from'] * 1.2)
        else:
            predictioned_salaries.append(salary['to'] * 0.8)
    return predictioned_salaries


def get_salaries_bracket(language, url):
    salaries_bracket = []
    for page in count(0):
        vacations = search_vacations(language, url, page=page)
        for salary in vacations['items']:
            salaries_bracket.append(salary['salary'])
        if page >= vacations['pages']: break
    return salaries_bracket


def average_salaries(programming_languages, url):
    vacancies_jobs = {}
    for language in programming_languages:
        predictioned_salaries = predict_rub_salary(language, url)
        vacancies_jobs[language] = {
            'vacancies_found': search_vacations(language, url)['found'],
            'vacancies_processed': len(predictioned_salaries),
            'average_salary': int(numpy.mean(predictioned_salaries))
        }
    pprint.pprint(vacancies_jobs)


url = 'https://api.hh.ru/vacancies/'
programming_languages = ['Python', 'Java', 'Javascript', 'Go', 'Scala', 'Ruby', 'C++', 'PHP']
average_salaries(programming_languages, url)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-27
@Vindicar

Paste the error into google translate.
TypeError: 'NoneType' object is not subscriptable - you are trying to access a dictionary/list (i.e. using square brackets), but you get None instead. See the full traceback of the error, on which line it occurs, determine which dictionary you are missing, and either fix the cause or add a check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question