I am trying to bypass this error: ItemNotFoundError: insufficient items with name u'No_Thanks' error by using try..except statement. However, I am getting another error saying: NameError: name 'ItemNotFoundError' is not defined. I am not sure why is this happening. Thanks. Here is the code that I am using
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1;Trident/5.0)')]
urls = "http://shop.o2.co.uk/mobile_phone/pay_monthly/init/Samsung/Galaxy_Ace_Purple"
r = br.open(urls)
page_child = br.response().read()
soup_child = BeautifulSoup(page_child)
contracts = [tag_c['value']for tag_c in soup_child.findAll('input', {"name": "tariff-duration"})]
data_usage = [tag_c['value']for tag_c in soup_child.findAll('input', {"name": "allowance"})]
for contract in contracts:
if contract <>"Pay_and_Go":
for data in data_usage:
br.select_form('formDuration')
br.form['tariff-duration']=[contract,]
try:
br.form['allowance']=[data,]
except ItemNotFoundError:
continue
br.submit()
page_child_child = br.response().read()
soup_child_child = BeautifulSoup(page_child_child)
items = soup_child_child.findAll('div', {"class": "n-pay-today"})
I’m guessing the exception is defined by
mechanize. Try:except mechanize.ItemNotFoundErrorIt appears that this is correct after installing mechanize: