是“>” (U + 003E GREATER-THAN SIGN)允许在html元素属性值内? [英] Is ">" (U+003E GREATER-THAN SIGN) allowed inside an html-element attribute value?
问题描述
/< tag [^>]>。*?< \ / tag> /
regex来匹配 tag
html元素不包含嵌套 tag
元素? 例如(lt.html):
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 / / ENhttp://www.w3.org/TR/html4/strict.dtd\">
< html>
< head>
< title>大于登录属性值< / title>
< / head>
< body>
< div> 1< / div>
< div title =>> 2< / div>
< / body>
< / html>
正则表达式:
<如果m〜< div [^>]>(。*?)< / div>〜lt.html
< / $ c $,则code> $ perl -nE c>
# !/ usr / bin / env python
import sys
导入BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(sys.stdin)
for soup in soup.findAll(' div'):
print div.string
$ python lt.py< lt.html
两者的输出相同:
1
> ; 2
预期产出:
1
2
w3c 说:
属性值是文本
和字符引用的混合,除了
是
文本不能包含的附加限制不明确的
符号。
,只发出警告)。
未转义<
和>
也被允许在注释中,所以这样简单的正则表达式可以被愚弄。
如果BeautifulSoup没有处理这个,它可能是一个错误或者可能是有意识的设计决策,使其更具弹性以避免属性中的结尾引号。
In other words may one use /<tag[^>]*>.*?<\/tag>/
regex to match the tag
html element which does not contain nested tag
elements?
For example (lt.html):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>greater than sign in attribute value</title>
</head>
<body>
<div>1</div>
<div title=">">2</div>
</body>
</html>
Regex:
$ perl -nE"say $1 if m~<div[^>]*>(.*?)</div>~" lt.html
And screen-scraper:
#!/usr/bin/env python
import sys
import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup(sys.stdin)
for div in soup.findAll('div'):
print div.string
$ python lt.py <lt.html
Both give the same output:
1
">2
Expected output:
1
2
w3c says:
Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand.
Yes, it is allowed (W3C Validator accepts it, only issues a warning).
Unescaped <
and >
are also allowed inside comments, so such simple regexp can be fooled.
If BeautifulSoup doesn't handle this, it could be a bug or perhaps a conscious design decision to make it more resilient to missing closing quotes in attributes.
这篇关于是“>” (U + 003E GREATER-THAN SIGN)允许在html元素属性值内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!