Unable to Debug

David Froger david.froger.ml at mailoo.org
Mon Jan 2 04:43:44 EST 2017


Hello Aritra,

Your standard output and standard error are mixed (I don't know why), so error
message is not immediate to read. It is:

> Traceback (most recent call last):
>   File "C:/Users/Aritra Bhattacharjee/PycharmProjects/PythonWebModules/Web Scraper.py", line 17, in <module>
>     print(product_name[i])
> KeyError: 20

There no such `20` title in `product_name`, so we can deduce that
`soup.find_all` found only 19 titles.

You can to instead:

    product_name = []  # a list instead of a dict is enought

    for title in soup.find_all('p', class_="product-title"):
        product_name.append(title.string)

    # or this should work too:
    # product_name = list(soup.find_all('p', class_="product-title"))

    for title in product_name:  # no need of i variable
        print(title)

Quoting Aritra Bhattacharjee (2017-01-02 10:04:59)
> 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:
> 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
> Intex Desire BT Over Ear Wired With Mic Headphone Black
> JBL T450 On Ear Wired Headphones With Mic Black
> 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
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list