'Get a specified number of results for a query'

def _get_n_results(self, query, n):

    videos = []
    limit = n
    url_query = {u'search_query': query.encode(u'utf-8')}
    url_query.update(self._EXTRA_QUERY_ARGS)
    result_url = (u'https://www.youtube.com/results?' + compat_urllib_parse_urlencode(url_query))
    for pagenum in itertools.count(1):
        data = self._download_json(result_url, video_id=(u'query    "%s"' % query), note=(u'Downloading    page    %s' % pagenum), errnote=u'Unable    to    download    API    page', query={u'spf': u'navigate'})
        html_content = data[1][u'body'][u'content']
        if (u'class="search-message' in html_content):
            raise ExtractorError(u'[youtube]    No    video    results', expected=True)
        new_videos = self._ids_to_results(orderedSet(re.findall(u'href="/watch\\?v=(.{11})', html_content)))
        videos += new_videos
        if ((not new_videos) or (len(videos) > limit)):
            break
        next_link = self._html_search_regex(u'href="(/results\\?[^"]*\\bsp=[^"]+)"[^>]*>\\s*<span[^>]+class="[^"]*\\byt-uix-button-content\\b[^"]*"[^>]*>Next', html_content, u'next    link', default=None)
        if (next_link is None):
            break
        result_url = compat_urlparse.urljoin(u'https://www.youtube.com/', next_link)
    if (len(videos) > n):
        videos = videos[:n]
    return self.playlist_result(videos, query)
