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

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

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

firstUrl = input('Enter - ')
linkPosition = int(input('Enter Link Position: ')) - 1
repete = int(input('Enter How many repete you want: '))
# Retrieve all of the anchor tags

tags = None
newUrl = None
i = 0
while(i < repete):

    if tags is None:
        url = firstUrl
    else:
        url = newUrl

    html = urllib.request.urlopen(url, context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    tags = soup('a')
    newUrl = tags[linkPosition].get('href', None)
    print(newUrl)
    print(tags[linkPosition].contents[0])
    i = i+1
