<p>我使用HTMLParser(python2.7)来解析使用urllib2下拉的页面,当我想将数据存储到<strong>feed</strong>方法的列表中时,会遇到AttributeError异常。但是,如果注释掉<strong>\uuqinit\uu</strong>方法,则异常就消失了</p>
<hr/>
<h3>在主.py</h3>
<pre><code># -*- coding: utf-8 -*-
from HTMLParser import HTMLParser
import urllib2
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class MyHTMLParser(HTMLParser):
def __init__(self):
self.terms = []
self.definitions = []
def handle_starttag(self, tag, attrs):
# retrive the terms
if tag == 'div':
for attribute, value in attrs:
if value == 'word':
self.terms.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(attrs[1][1])
# retrive the definitions
if value == 'desc':
if attrs[1][1]:
self.definitions.append(attrs[1][1])
else:
self.definitions.append(None)
parser = MyHTMLParser()
# open page and retrive source page
response = urllib2.urlopen('http://localhost/')
html = response.read().decode('utf-8')
response.close()
# extract the terms and definitions
parser.feed(html)
</code></pre>
<hr/>
<h3>输出</h3>
^{pr2}$