BUG触发时的完整报错内容(本地无关路径用已经用 **** 隐去):

**************\lib\site-packages\bs4\builder\_htmlparser.py:78: UserWarning: unknown status keyword 'end ' in marked section
warnings.warn(msg)
Traceback (most recent call last):
File "**************/test.py", line 5, in <module>
bs = BeautifulSoup(html, 'html.parser')
File "**************\lib\site-packages\bs4\__init__.py", line 281, in __init__
self._feed()
File "**************\lib\site-packages\bs4\__init__.py", line 342, in _feed
self.builder.feed(self.markup)
File "**************\lib\site-packages\bs4\builder\_htmlparser.py", line 247, in feed
parser.feed(markup)
File "D:\Program Files\Python37\lib\html\parser.py", line 111, in feed
self.goahead(0)
File "D:\Program Files\Python37\lib\html\parser.py", line 179, in goahead
k = self.parse_html_declaration(i)
File "D:\Program Files\Python37\lib\html\parser.py", line 264, in parse_html_declaration
return self.parse_marked_section(i)
File "D:\Program Files\Python37\lib\_markupbase.py", line 160, in parse_marked_section
if not match:
UnboundLocalError: local variable 'match' referenced before assignment

在解析HTML时,标签开始部分使用形如 <!-[if IE eq 9]> 的浏览器判断标识符,结束时结束标签<![end if]->(正确的开始和结束标签应该为<!--[if IE 9]><![endif]-->)无法正常匹配关闭即可触发。

触发BUG的示例代码如下:

from bs4 import BeautifulSoup

html = """
<!-[if IE eq 9]>
<a href="https://www.shwww.net/">https://www.shwww.net/</a>
<![end if]->
""" bs = BeautifulSoup(html, 'html.parser')

在 Python 3.7.0 版本中,触发BUG部分的代码存在于 \Lib\_markupbase.py 中的 146 行的 parse_marked_section 方法,该方法代码如下:

https://github.com/python/cpython/blob/bb9ddee3d4e293f0717f8c167afdf5749ebf843d/Lib/_markupbase.py#L160

    def parse_marked_section(self, i, report=1):
rawdata= self.rawdata
assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
sectName, j = self._scan_name( i+3, i )
if j < 0:
return j
if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:
# look for standard ]]> ending
match= _markedsectionclose.search(rawdata, i+3)
elif sectName in {"if", "else", "endif"}:
# look for MS Office ]> ending
match= _msmarkedsectionclose.search(rawdata, i+3)
else:
self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
if not match:
return -1
if report:
j = match.start(0)
self.unknown_decl(rawdata[i+3: j])
return match.end(0)

由于错误的HTML代码未正确关闭,使得流程判断既没有进入 if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:

elif sectName in {"if", "else", "endif"}: ,而是报出一个错误 UserWarning: unknown status keyword 'end ' in marked section warnings.warn(msg) 后执行到 if not match ,而此时 match 未申明,故而触发错误。

此BUG存在于多个Python版本中,修复方法,在 if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}: 之前预定义一个match变量即可:

https://github.com/python/cpython/blob/bb9ddee3d4e293f0717f8c167afdf5749ebf843d/Lib/_markupbase.py#L152

    def parse_marked_section(self, i, report=1):
rawdata= self.rawdata
assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
sectName, j = self._scan_name( i+3, i )
if j < 0:
return j
match = None
if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:
# look for standard ]]> ending
match= _markedsectionclose.search(rawdata, i+3)
elif sectName in {"if", "else", "endif"}:
# look for MS Office ]> ending
match= _msmarkedsectionclose.search(rawdata, i+3)
else:
self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
if not match:
return -1
if report:
j = match.start(0)
self.unknown_decl(rawdata[i+3: j])
return match.end(0)

_markupbase.py if not match: UnboundLocalError: local variable 'match' referenced before assignment,分析Python 库 html.parser 中存在的一个解析BUG的更多相关文章

  1. RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment

    Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...

  2. UnboundLocalError: local variable 'range' referenced before assignment

    1. 报错信息 UnboundLocalError: local variable 'range' referenced before assignment 2. 代码 class Car(): &q ...

  3. 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment

    闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...

  4. 变量引用的错误:UnboundLocalError: local variable 'range' referenced before assignment

    class Battery(): """一次模拟电瓶汽车的简单尝试""" def __init__(self,battery_size = ...

  5. 出现UnboundLocalError: local variable 'a' referenced before assignment异常的情况与解决方法

    出现UnboundLocalError: local variable ‘a’ referenced before assignment异常的情况与解决方法字面意思:局部变量赋值前被引用原因:局部变量 ...

  6. 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment

    总结: 内部函数,不修改全局变量可以访问全局变量 内部函数,修改同名全局变量,则python会认为它是一个局部变量 在内部函数修改同名全局变量之前调用变量名称(如print sum),则引发Unbou ...

  7. python:UnboundLocalError: local variable 'xxx' referenced before assignment

    近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...

  8. UnboundLocalError: local variable ‘xxx‘ referenced before assignment

    原因 在Python函数中调用了某个和全局变量同名的局部变量,导致编译器不知道此时使用的是全局变量还是局部变量 a = 3 def func(): a+=3 func() UnboundLocalEr ...

  9. UnboundLocalError: local variable 'f' referenced before assignment

    参考方案链接: 1.http://blog.chinaunix.net/uid-631981-id-3766212.html 2.http://blog.sina.com.cn/s/blog_4b9e ...

随机推荐

  1. c# 命令行下编译c#文件 // c# file类读写文件

    c# 命令行下编译c#文件 2010-03-01 15:02:14|  分类: c# 学习|字号 订阅     在 开始  ——>程序 ——>vstool中打开vs2008命令提示. 通过 ...

  2. .NET页面事件执行顺序

    摘自:http://www.cnblogs.com/kenkofox/archive/2011/03/18/1987998.html和http://blog.csdn.net/yiruoyun/art ...

  3. git 设定全局ignore

    创建: 2017/08/08   位置: $HOME/.config/git/ignore git/ignore 要自建 内容  https://github.com/github/gitignore ...

  4. Cent OS 6/7 中通过yum安装软件时提示cannot find a valid baseurl...的解决方法

    目录 1 问题描述 2 解决方法一 (Cent OS 7中有效) 3 解决方法二 (Cent OS 7中无效) 1 问题描述 新申请了虚拟机, 系统版本是Cent OS 7.2. 在安装软件的过程中, ...

  5. Android PopWindow的替代品BasePopup

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/109 背景描述 最近一段时间,又看到了这个开源项目Base ...

  6. C# 取两位小数

    double s=0.55555;result=s.ToString("#0.00");//点后面几个0就保留几位 如果要四舍五入的话,用这个double dbdata = 0.5 ...

  7. Jenkins构建项目,JAVA_HOME is not defined correctly

    好久都没有更新了,由于职位调整,开始捣鼓持续集成的东西了.jenkins的基本安装配置网上有很多教程,不用多讲了,就记录下我在使用过程中遇到的一些问题.话说这个jenkins环境以及安装好了有一段时间 ...

  8. java 实现将java对象转为yaml文件

    首先我们建两个类,以下两个类展示的是一个学生拥有多个手机号码联系人. 先是学生类: package com.ming.yaml.beans; import java.util.ArrayList; i ...

  9. 苹果html上传后图片旋转问题

    最近做移动web项目但是遇到在苹果设备上html上传图片后,图片传到后台是旋转的 旋转角度不一,因此再次 读取照片时,无法正常显示,目前已经找到解决方法,至于原因看不太懂 翻译过来也是完全按照单词翻译 ...

  10. easyui验证提示框 卡在屏幕上!!

    场景:验证提示框,关闭diglog窗口后 还显示在页面中 解决方法: 在窗口关闭事件中,删除提示框(这貌似并不可行),只能将验证提示框隐藏起来. $('#dialog').dialog({ onClo ...