如何告诉python HTMLParser停止

车站

我有一个用例,告诉您何时是标签link及其属性,rel=dns-prefetch然后才说启用了预解析dns。

我做了一个标志,pre_resolve_dns_enabled并将其设置为true,如下所示。

class Extractor(HTMLParser):

    def __init__(self):
        HTMLParser.__init__(self)
        self.pre_resolve_dns_enabled = False

    def feed(self, data):
        HTMLParser.feed(self,data)

    def handle_starttag(self, tag, attrs):
        if tag == 'link' and ('rel', 'dns-prefetch') in attrs:
            self.pre_resolve_dns_enabled = True
            #Now if one dns is resolved so whole domain remains resolved , how do I tell the parser to abort now , leaving the flag to true.

有什么帮助吗?

埃里克

HTMLParser并非旨在停止。为此,您想使用流解析器,例如xml.saxxml.etree.cElementTree

消化整个HTML文件真的有问题吗?预期的用例如下:

extractor = Extractor()
... feed html to extractor using one or more .feed() calls ...
extractor.close()

if extractor.pre_resolved_dns_enabled:
  ...
else:
  ...

如果确实存在问题,则可以将输入的HTML分成多个部分,并提供给它们,直到找到您的标签为止,例如:

html = ...the html to parse...
chunks = [ html[i:i+1024] for i in xrange(0, len(html), 1024) ]
extractor = Extractor()
for c in chunks:
  if extractor.pre_resolved_dns_enabled:
    break
  extractor.feed(c)
extractor.close()
# check extractor.pre_resolved_dns_enabled

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何告诉 Python 在源文件中间停止解析?

如何告诉phpunit停止失败

python htmlparser'&'字符

Python HTMLParser对象删除

如何告诉 sed 在匹配的单词之后停止?

Python HTMLParser打印出空行

我如何告诉Chrome停止自动完成网站建设?

如何根据损失值告诉Keras停止训练?

如何告诉trello停止使短语成为超链接?

如何告诉systemd在不再需要/不再需要时停止服务

如何使用python HTMLParser库从特定的div标签提取数据?

如何使用python HTMLParser从HTML页面中抓取特定值

Python 3.9 和 Pycharm、HTMLParser AttributeError

HTMLParser中的Python可重写函数

测试结束后,如何告诉GoogleMock停止检查期望值?

如何告诉Puppet在关机时停止服务而不保持其运行?

Eclipse试图在我的.svn目录中构建文件...如何告诉它停止?

每当按下任意键时,如何告诉bash top停止回显文本?

如何告诉Eclipse Java Formatter(Ctrl + shift + F)停止删除空格

我如何告诉Rust中的std :: io :: copy停止阅读并完成写作?

如何告诉PhpStorm停止使用外部编辑器打开文件?

如何告诉自定义onSuccess函数停止Ant Design Upload组件中的Upload Spinner?

如何告诉 apt-get install 停止尝试设置 openssh-server?

如何告诉git停止显示处于git状态的文件,直到再次对其进行修改?

如何告诉Angular 2自动停止设置Content-Type标头?

OR-TOOLS Google如何告诉求解器在达到特定结果时停止

我如何告诉Homebrew每次我要安装某些东西时都停止运行brew更新?

如何使用 htmlparser2 转换 html 中的内容

使用python中的HTMLParser解析html中的特定链接?