Unable to Debug

rus.cahimb at gmail.com rus.cahimb at gmail.com
Mon Jan 2 04:43:42 EST 2017


On Monday, January 2, 2017 at 2:35:22 PM UTC+5:30, Aritra Bhattacharjee wrote:
> I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com .

[RM]: Welcome and have fun.

> for i in range(1,21):
>     print(product_name[i])
> 
> 
> Output:
> Traceback (most recent call last):
> Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black)
> Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue
> Sony MDR-ZX110A Headphones Without Mic (White)
> Philips SBCHL140/98 Over Ear Headphone Without Mic
>   File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in <module>
> Intex Desire BT Over Ear Wired With Mic Headphone Black
>     print(product_name[i])
> JBL T450 On Ear Wired Headphones With Mic Black
> KeyError: 20
. . . . 
. . . . 
> The Output shows some error that I could not understand why...Thanks in Advance

[RM]: You should read tracebacks stack from the bottom. Initially there will be hiccups but over a period of time you will learn.

>From your code snippet: for loop is supplied with a range of 1 to 21 and while the key value (variable, i) is 20, print fails there. In the previous loop, where you are using methods in BeautifulSoup, are you sure that many items are added to the list, product_name.

If I were you, in the loop where the list is expanded by adding the product title names, will rename the variable, i, as 'product_counter'. And, in the last for loop, this counter can be used in the rangem like, (0, product_counter).

All the best,
/Ram



More information about the Python-list mailing list