gpt4 book ai didi

python - python中错误处理的问题

转载 作者:行者123 更新时间:2023-12-03 08:04:32 25 4
gpt4 key购买 nike

我试图绕过这个错误: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"})

最佳答案

我猜这个异常是由 mechanize 定义的.试试:except mechanize.ItemNotFoundError
安装 Mechanize 后,这似乎是正确的:

>>> import mechanize
>>> print mechanize.ItemNotFoundError
<class 'mechanize._form.ItemNotFoundError'>
>>> print mechanize.__version__
(0, 2, 5, None, None)

关于python - python中错误处理的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com