I have this task for a python class where I need to begin from a particular link at a particular position, at that point follow that link a particular number of times. Apparently, the primary link has position 1. This is the link.
traceback error picture I experience difficulty with finding the link, the error
"index out of range"
comes out. would anyone be able to assist with sorting out some way to find the connection/position? This is my code:
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
count = int(raw_input('Enter count: '))+1
position = int(raw_input('Enter position: '))
tags = soup('a')
tags_lst = list()
for tag in tags:
needed_tag = tag.get('href', None)
tags_lst.append(needed_tag)
for i in range(0,count):
print 'retrieving: ',tags_lst[position]
I also tried this code:
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
count = int(raw_input('Enter count: '))+1
position = int(raw_input('Enter position: '))
tags = soup('a')
tags_lst = list()
for tag in tags:
needed_tag = tag.get('href', None)
tags_lst.append(needed_tag)
for i in range(0,count):
print 'retrieving: ',tags_lst[position]
position = position + 1
I'm actually getting different links than the ones in the model anyway when I print the entire list of connections the positions coordinate so I don't have a clue. Exceptionally unusual.