我试图绕过这个错误:ItemNotFoundError: insufficient items with name u'No_Thanks'
使用 try..except 语句出错。但是,我收到另一个错误消息:NameError: name 'ItemNotFoundError' is not defined
. 我不确定为什么会这样。谢谢。这是我正在使用的代码
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"})