python - MyHTMLParser 实例没有属性 'intitle'

标签 python

我正在尝试使用 HTMLParser 从 HTML 页面读取标题标签。但是我收到上述错误。我的类(class)如下所示:

readtitle.py

from HTMLParser import HTMLParser
import urllib


class MyHTMLParser(HTMLParser):
    def __init__(self, url):
        HTMLParser.__init__(self)        
        self.url = url
        self.data = urllib.urlopen(url).read() 
        self.feed(self.data)
        self.intitle = ""
        self.mytitle = ""

    def handle_starttag(self, tag, attrs):
        self.intitle = tag == "title"

    def handle_data(self, data): 
        if self.intitle:
            self.mytitle = data
            return self.mytitle

我使用以下命令运行代码并收到错误:

import urllib
import readtitle
parser = readtitle.MyHTMLParser("http://docs.python.org/tutorial/classes.html")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "readtitle.py", line 10, in __init__
 self.feed(self.data)
File "/usr/lib/python2.6/HTMLParser.py", line 108, in feed
 self.goahead(0)
File "/usr/lib/python2.6/HTMLParser.py", line 142, in goahead
 if i < j: self.handle_data(rawdata[i:j])
File "readtitle.py", line 18, in handle_data
if self.intitle:
AttributeError: MyHTMLParser instance has no attribute 'intitle'

最佳答案

在运行 self.intitle = ""之前,您运行 self.feed(),从而调用 handle_data() (根据跟踪判断)
修复:

  self.url = url
  self.data = urllib.urlopen(url).read() # Perhaps there should be a decode() here?
  self.intitle = False
  self.mytitle = "" 
  self.feed(self.data)

----------------------------------------------------

调试始终是最重要的部分。运行此代码并查看它打印的内容。

from HTMLParser import HTMLParser
import urllib, sys

class MyHTMLParser(HTMLParser):
  def __init__(self, url):
    HTMLParser.__init__(self)        
    self.url = url
    self.data = urllib.urlopen(url).read()
    self.in_title = False
    self.title = ''
    self.feed(self.data)
  def handle_starttag(self, tag, attrs):
    if tag == 'body': sys.exit('Found <body>, quitting') # Much easier to look at
    self.in_title = (tag == 'title')
    print 'Handled start of', tag, '  in_title is', self.in_title
  def handle_endtag(self, tag):
    print 'Handled end of', tag
  def handle_data(self, data):
    print "Handling data:", repr(data)
    if self.in_title:
        print "Apparently, we are in a <title> tag. self.title is now", repr(data)
        self.title = data
        print data
        return self.title

parser = MyHTMLParser("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")

为方便起见,相关页面的 HTML:

<HMTL>
<HEAD>
<TITLE>webpage1</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFf" LINK="006666" ALINK="8B4513" VLINK="006666">

关于python - MyHTMLParser 实例没有属性 'intitle',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11409945/

相关文章:

python - 如何强制caffe读取所有训练数据?

python - 如何替换 Python 字符串中的单词

python - 使用 LXML 仅按属性查询的 CSS 选择器

python - 屏蔽图像,其中屏蔽像素存在于像素值列表中

python - 是否有可能在 ffmpeg 命令中引发错误?

python - 无法比较类型 'ndarray(dtype=int64)' 和 'str'

python - 自定义 QSizeGrip 以调整 QListWidget 的大小

python - python 解释器中 'enter' 键的替代方案?

python - 深拷贝有问题吗?

python - 如何在任意位置插入元素到列表中?