# To run this, you can install BeautifulSoup
# https://pypi.python.org/pypi/beautifulsoup4

# Or download the file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import re

def main():
    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE

    url = input('Enter - ')
    count = int(input('Enter count: '))
    position = int(input('Enter position: '))
    page_count = 0
    while page_count < count:
        html = urllib.request.urlopen(url, context=ctx).read()
        soup = BeautifulSoup(html, 'html.parser')

        # Retrieve all of the anchor tags
        tags = soup('a')
        url = tags[position-1].get('href', None)
        print('Retrieving: ' + re.findall('http://py4e-data.dr-chuck.net/known_by_(.+).html', url)[0])
        page_count += 1

if __name__ == "__main__":
    main()