请考虑以下代码:
for url in urls:
obj = HtmlInfo()
obj.url = url
obj.html = hc.get_html(url)
obj.tag_count = hc.get_num_tags(obj.html, 0, True)
htmlinfos.append(obj)
其中urls
是一个网址列表,htmlinfos
被初始化为一个空列表,就像这样 - 在循环之前,当然:
htmlinfos = [ ]
然而,无论出于何种原因,当我尝试运行此代码时,我收到list-assignment index out of range
异常。
这可能是什么问题?注意,我的Python版本是2.7,我使用的是最新的稳定版Django(1.4,我相信)
更新 - 追溯
Environment:
Request Method: GET
Request URL: http://xx.xxx.xxx.xx/xxx/0/test/
Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'scrapper',
'django_pdb')
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_pdb.middleware.PdbMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in test
44. return HttpResponse("Dis be er bad query yo " + test_id )
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in __get_html_list
23. return list
Exception Type: IndexError at /xxx/0/test/
Exception Value: list assignment index out of range
更新 - __get_html_list()
def __get_html_list():
hc = HtmlCounter()
htmlinfos = [ ]
#add more urls here for testing
urls = [ '/usr/share/nginx/www/xxx/private/template/test/html_count_test.html' ]
for url in urls:
obj = HtmlInfo()
obj.url = url
obj.html = hc.get_html(url)
obj.tag_count = hc.get_num_tags(obj.html)
htmlinfos.append(obj)
return htmlinfos
注意
原来htmlinfos
简称为list
,所以在我发布之前我改变了它,重新开始,我仍然得到同样的错误:/
更新 - get_html_tag_count()
为了简洁起见,我想我也可以发布这个,以防这可能与问题有关:
def get_num_tags(self, html):
if reset:
self.reset()
current_index = 0
for char in html:
if (char == "<"):
close_index = html[current_index:].find("/>", current_index)
if close_index == -1:
break
else:
++self._tag_count
++current_index
return self._tag_count
答案 0 :(得分:2)
尝试分别用++self._tag_count
和++current_index
替换self._tag_count+=1
和current_index+=1
++var
适用于大多数语言,但python与大多数语言不同。