Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

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. 

1 Answer

0 votes
by (26.4k points)

Hey! I needed to work on a comparable exercise, and in light of the fact that I had a few questions, I discovered your inquiry. Here is my code and I think it works. I trust it will be useful for you

import urllib

from bs4 import BeautifulSoup

url = 'http://py4e-data.dr-chuck.net/known_by_Fikret.html'

html = urllib.urlopen(url).read()

soup = BeautifulSoup(html, 'html.parser')

count = 8

position = 18

tags_lst = []

for x in xrange(count-1):

    tags = soup('a')

    my_tags = tags[position-1]

    needed_tag = my_tags.get('href', None)

    tags_lst.append(needed_tag)

    url = str(needed_tag)

    html = urllib.urlopen(url).read()

    soup = BeautifulSoup(html, 'html.parser')

Join the python online course fast, to learn python concepts in detail and get certified.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...