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 - 使用 del 和切片是否会先创建一个新对象,然后再删除?会就位吗?

python - 如何从 Python 列表中提取元素同时考虑提取元素的位置?

Python 字符串操作。第五位加一个角色

python - 重新启动女服务员服务

python - Python 的 rdb 调试断点处不存在局部变量(在 celery 任务中)

python - python 对包含字符串和整数的列表中的元素求和,并将答案放入另一个列表中

python - 如何使用 os.listdir 在 Python3 中获取文件信息?

python - Python 中的时间范围重叠算法

python - 检查字符串是否在 python 中的 2-GB 字符串列表中

python - 如何将 numpy 字符串数组(带逗号)保存到 CSV?