Unable to Debug

Vincent Vande Vyvre vincent.vande.vyvre at telenet.be
Mon Jan 2 04:32:34 EST 2017


Le 02/01/17 à 10:04, Aritra Bhattacharjee a écrit :
> 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

Try with:


for i in product_name:

     print(i)


Vincent




More information about the Python-list mailing list