# To run this, download the BeautifulSoup zip file
# http://www.py4e.com/code3/bs4.zip
# or pip install beautifulsoup4 to ensure you have the latest version
# and unzip it in the same directory as this file
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl # defauts to certicate verification and most secure protocol (now TLS)
# Ignore SSL/TLS certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
#Follow link at below position
count=17
#Follow link this many times
times=6
# Retrieve all of the anchor tags
tags = soup('a')
lst = list()
for tag in tags:
#print(tag.get('href', None))
lst.append(tag.get('href', None))
print(lst[count])
for i in range(times):
url = lst[count]
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the anchor tags
tags = soup('a')
lst = list()
for tag in tags:
#print(tag.get('href', None))
lst.append(tag.get('href', None))
print(lst[count])