The problem is that although you print out the 18th tags
entry, you don't set url
to this value. Unfortunately, you use url
also within your tags
loop, so you don't notice this mistake. In your code, url
is still set to the last entry of tags
. If you print out I
with the actual url
used in your loop (uncomment the appropriate lines), you will see this:
0 http://py4e-data.dr-chuck.net/known_by_Shannon.html
['Riya', 'Terri', 'Coban', 'Oswald', 'Codie', 'Arshjoyat', 'Carli', 'Aieecia', 'Ronnie', 'Yelena', 'Abid', 'Prithvi', 'Ellenor', 'Shayla', 'Chala', 'Nelson', 'Chaitanya', 'Stacey', 'Karis', 'Mariyah', 'Jamie', 'Breeanna', 'Kendall', 'Adelaide', 'Aimiee', 'Manwen', 'Dennys', 'Benjamyn', 'Reynelle', 'Jesuseun', 'Malik', 'Brigitte', 'Farah', 'Youcef', 'Ruqayah', 'Mili', 'Caitaidh', 'Raul', 'Katelyn', 'Yakup', 'Cohan', 'Lylakay', 'Dougray', 'Silvana', 'Roxanne', 'Tanchoma', 'Andie', 'Aarman', 'Kyalah', 'Tayyab', 'Malikah', 'Bo', 'Oona', 'Daniil', 'Wardah', 'Jessamy', 'Karly', 'Tala', 'Ilyaas', 'Maram', 'Ruaidhri', 'Donna', 'Liza', 'Aileigh', 'Muzzammil', 'Chi', 'Serafina', 'Abbas', 'Rhythm', 'Jonny', 'Niraj', 'Ciara', 'Kylen', 'Demmi', 'Christianna', 'Tanzina', 'Brianna', 'Kevyn', 'Hariot', 'Maisie', 'Naideen', 'Nicolas', 'Suvi', 'Areeb', 'Kiranpreet', 'Rachna', 'Umme', 'Caela', 'Miao', 'Tansy', 'Miah', 'Luciano', 'Karolina', 'Rivan', 'Cavan', 'Benn', 'Haydn', 'Zaina', 'Rafi', 'Ahmad']
<a href="http://py4e-data.dr-chuck.net/known_by_Stacey.html">Stacey</a>
1 http://py4e-data.dr-chuck.net/known_by_Ahmad.html
['Tilhi', 'Rachel', 'Latif', 'Deryn', 'Pawel', 'Anna', 'Blake', 'Brehme', 'Jo', 'Laurajane', 'Khayla', 'Declyan', 'Graidi', 'Foosiya', 'Nabeeha', 'Otilija', 'Dougal', 'Adeena', 'Alfie', 'Angali', 'Lilah', 'Saadah', 'Kelam', 'Kensey', 'Tabitha', 'Peregrine', 'Abdisalam', 'Presley', 'Allegria', 'Harish', 'Arshjoyat', 'Hussan', 'Sammy', 'Ama', 'Leydon', 'Anndra', 'Anselm', 'Logyne', 'Fion', 'Jacqui', 'Reggie', 'Mounia', 'Pedro', 'Hussain', 'Raina', 'Inka', 'Shaylee', 'Riya', 'Phebe', 'Uzayr', 'Isobella', 'Abdulkadir', 'Johndean', 'Charlotte', 'Moray', 'Saraah', 'Liana', 'Keane', 'Maros', 'Robi', 'Rowanna', 'Wesley', 'Maddox', 'Annica', 'Oluwabukunmi', 'Jiao', 'Nyomi', 'Hamish', 'Bushra', 'Marcia', 'Rimal', 'Kaceylee', 'Limo', 'Dela', 'Cal', 'Rhudi', 'Komal', 'Stevey', 'Amara', 'Nate', 'Roma', 'Fatou', 'Marykate', 'Abiya', 'Bay', 'Kati', 'Carter', 'Niraj', 'Maisum', 'Jaz', 'Coban', 'Harikrishna', 'Armani', 'Muir', 'Ilsa', 'Benjamyn', 'Russel', 'Emerson', 'Rehaan', 'Veronica']
<a href="http://py4e-data.dr-chuck.net/known_by_Adeena.html">Adeena</a>
To avoid this problem, you have to set url
for the next loop to the 18th entry:
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
#import re
term_counter = (0)
file = list()
#regex = list()
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
for _I in range(7) :
#print(_I, url) <- this prints out the _I value of the loop and the url used in this round
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')
#print([item.contents[0] for item in tags]) <- this prints out a list of all names on this page
file = list()
for tag in tags :
file.append(tag)
#this is the last url you used in your code for the next _I loop
url = tag.get('href')
#so we have to redefine url as the 18th entry in your list for the next _I loop round
url = file[17].get("href")
print("The next url we will use is {}".format(url))