Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python HTMLPasser-仅提取img标记_Python_Html - Fatal编程技术网

Python HTMLPasser-仅提取img标记

Python HTMLPasser-仅提取img标记,python,html,Python,Html,我使用HTMLPasser从简单的html文本中提取图像url,如下所示: html = <p><span style="font-size: 17px;"><span style="color: #993300;"><img style="margin-right: 15px; vertical-align: top;" src="images/announcements.png" alt="announcements" /><cite&g

我使用HTMLPasser从简单的html文本中提取图像url,如下所示:

html = <p><span style="font-size: 17px;"><span style="color: #993300;"><img style="margin-right: 15px; vertical-align: top;" src="images/announcements.png" alt="announcements" /><cite>some message I would like to preserve with its formatting</cite></span></span></p>
现在我还需要一个没有img标记的html版本,但是我很难在正确的位置关闭标记。以下是我尝试过的:

class MyHtmlParser(HTMLParser):
    '''
    Parse simple url to extract data and image url.
    This is expecting a simple url containing only one data block and one iimage url.
    '''
    def __init__(self):
        HTMLParser.__init__(self)
        self.noImgHtml = ''

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            for a in attrs:
                if a[0] == 'src':
                    self.imageUrl = a[1]
        else:
            print '<%s>' % tag
            self.noImgHtml += '<%s>' % tag
            for a in attrs:
                print '%s=%s' % a
                self.noImgHtml += '%s=%s' % a

    def handle_endtag(self, tag):
        self.noImgHtml += '</%s>' % tag

    def handle_data(self, data):
        self.noImgHtml += data
MyHtmlParser.feedhtml的输出如下:

<b>LATEST NEWS:</b><p><span>style=font-size: 17px;<span>style=color: #993300;</img><cite>The image uploader works again, so make sure to use some screenshots in your uploads/tutorials to make your submission look extra nice</cite></span></span></p>
正如您所看到的,正如我的代码流所期望的那样,标记并不像在原始html中那样关闭,例如span>

这可以用HTMLPasser轻松完成吗?或者我应该求助于RE来提取看起来不太优雅的图像标签吗

我不能使用外部模块来实现这一点,因此需要使用HTMLPasser提供的功能

提前感谢,,
frank

事实上,您的代码正在运行,您可以使用

parser = MyHtmlParser()
parser.feed(html)
parser.noImgHtml
这才是你真正想要的。我试过了,结果是

<p><span>style=font-size: 17px;<span>style=color: #993300;</img><cite>some message I would like to preserve with its formatting</cite></span></span></p>
排除img的结束标记

事实上,MyHtmlParser.feedhtml只打印结果,不返回任何结果。原因 如果未在handle_endtag和handle_数据中打印endtag和标记内容,则打印输出中的标记未正确关闭

如果您试图处理嵌套div,这里的答案可能会有所帮助。
.

HTMLParser.get\u starttag\u文本似乎是重建原始html的门票。这似乎有效:

class MyHtmlParser(HTMLParser):
    '''
    Parse simple url to extract data and image url.
    This is expecting a simple url containing only one data block and one iimage url.
    '''
    def __init__(self):
        HTMLParser.__init__(self)
        self.noImgHtml = ''

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            for a in attrs:
                if a[0] == 'src':
                    self.imageUrl = a[1]
        else:
            self.noImgHtml += self.get_starttag_text()


    def handle_endtag(self, tag):
        if tag != 'img':
            self.noImgHtml += '</%s>' % tag

    def handle_data(self, data):
        self.noImgHtml += data
        self.text = data

谢谢,但这仍然不会使标签的属性格式正确,对吗?我刚刚偶然发现了HTMLParser.get_starttag_text,这似乎是我重建原始htmlI所需要的。我看到了这里的问题,你也可以调整handle_starttag方法,在其他部分,添加。但是显然应该使用get_starttag_text,因为我们不需要重新发明轮子。
class MyHtmlParser(HTMLParser):
    '''
    Parse simple url to extract data and image url.
    This is expecting a simple url containing only one data block and one iimage url.
    '''
    def __init__(self):
        HTMLParser.__init__(self)
        self.noImgHtml = ''

    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            for a in attrs:
                if a[0] == 'src':
                    self.imageUrl = a[1]
        else:
            self.noImgHtml += self.get_starttag_text()


    def handle_endtag(self, tag):
        if tag != 'img':
            self.noImgHtml += '</%s>' % tag

    def handle_data(self, data):
        self.noImgHtml += data
        self.text = data