0

i write some code which return two output the error appears,what is the main problem of my code???????

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import os
import sys
import unicodecsv as csv
import codecs
from urllib.request import urlopen


for i in range(22):

    my_url = "https://www.bamilo.com/electronic_accessories/?source=gfm/?facet_is_mpg_child=0&viewType=gridView&page="

    uClient = uReq(my_url + str(i))

    page_html = uClient.read()

    uClient.close()

    page_soup = soup(page_html, "html.parser")

    containers = page_soup.findAll("div" , {"class" : "sku -gallery" })

    filename = "product.csv"
    f = codecs.open(filename, "a" , "utf-8-sig")
    headers = "price_two\n"
    f.write(headers)


    for container in containers:

        price_old = container.findAll("span",{"class" : "price -old "} )

        price_two = price_old[0].text.strip()

        print("price_two " + price_two)

        f.write(price_two.replace(",", "")  + "\n")

f.close()

ERORR :::

> price_two 1,800,000ریال
price_two 2,800,000ریال
Traceback (most recent call last):
  File "F:\bam.py", line 34, in <module>
    price_two = price_old[0].text.strip()
IndexError: list index out of range
jordanm
  • 26,799
  • 4
  • 56
  • 64
  • 1
    Welcome to SO! Please take the [tour], and try to format your question in a way that it is easy to read – Dux Jun 04 '18 at 19:48
  • As your error message suggests, `price_old` sometimes doesn't have a first element, i.e. it may be empty. – Dux Jun 04 '18 at 19:50

1 Answers1

1

Your container may sometimes not return any items for 'price -old'. In this case, price_old is an empty list, therefore the look-up of price_old[0] fails. Add a condition to check for this:

for container in containers:

    price_old = container.findAll("span",{"class" : "price -old "} )
    if len(price_old) > 0:
        price_two = price_old[0].text.strip()

        print("price_two " + price_two)

        f.write(price_two.replace(",", "")  + "\n")
Dux
  • 1,165
  • 9
  • 29
Vla Mai
  • 305
  • 2
  • 14