Unable to Debug

Aritra Bhattacharjee whoisaritra at gmail.com
Mon Jan 2 04:04:59 EST 2017


I am new to python programming. I wrote a code to search for the product names on a page of snapdeal.com .

Code:
import urllib.request
from bs4 import BeautifulSoup as BS

url = 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty'

response = urllib.request.urlopen(url).read()
soup = BS(response, 'html.parser')

#for dataprice in soup.find_all('span', class_="lfloat product-price"):
#    print(dataprice)
product_name={}
i=0
for title in soup.find_all('p', class_="product-title"):
        product_name[i]=title.string
        i += 1
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
Motorola Pulse Max Over Ear Wired Headphones With Mic (Black)
Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic White
Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with Bluetooth & NFC (Black)
Intex JAZZ Over Ear Wired With Mic Headphone Black
Skullcandy S5GBW-J539 On Ear Wireless Headphones With Mic Black
JBL C300SI Over Ear Wired Without Mic Headphone Black
Zoook Rocker iFit Bluetooth Wireless Headphones With Mic Black
Signature VM-46 Over Ear Wired Headphone Without Mic White
Sony MDR-G45 Over Ear Wired Without Mic Headphone- Black
Motorola Pulse Max Over Ear Wired Headphones With Mic (White)
Bose SoundTrue Around-Ear Headphones with Mic (Navy Blue) for Samsung and Android Devices
JBL T450 On Ear Wired Headphones With Mic Blue
Motorola Pulse 2 Over Ear Wired Headphones With Mic (White)


The Output shows some error that I could not understand why...Thanks in Advance


More information about the Python-list mailing list