html 选中tag标签,HTML Tag Selector标签选择器PFC020071801
之前写过两篇关于HTML DOM解析的文章(附代码):
[PFC020071401](https://www.jianshu.com/p/46c92333e2c8)
[PFC0200512](https://www.jianshu.com/p/0a3603993864)
第一篇只能识别简单的HTML标签包含,第二篇仅可以分析出一组根叶子节点(Leaf Node),距离简单的标签选择器还有较大的差距。后来想了一两天,结合第二篇PFC020071401里的最小标签对称法,决定放弃完全依赖正则表达式的想法。最终考虑像进行语法分析那样,从HTML文本中,分离出一棵或多颗的语法树,从语法树中找出最终目标。
当前的想法是:
1)分析路径语法;
2)对HTML文本进行正则表达式匹配,找到目标标签;
3)使用最小标签对称法,循环匹配出所有的结果树;
4)判断当前标签是否为最终目标标签,是则利用第3)步结果进行步骤1),否则5);
5)列出结果。
最小标签对称法是利用HTML标签的开始标记和结束标记对称的原理,来完成标签完整性的目标的方法。对于某些个别标签开始和结束标记不完全对称的问题另当别论。HTML5基本要求标签对称。这样就可以忽略正则表达式贪婪匹配带来的“多重包含问题”--标签不完整、包含多个匹配结果。看以下代码最小对称法 minimalpair():
'''
author: MRN6
blog: qq_21264377@blog.csdn.net
updated: Jul. 18th, 2020 Sat. 03:40PM
'''
def minimalpair(tag, html, position):
#最小对称法
check_start_tag='
check_end_tag=''
if tag=='meta':
check_end_tag='>'
else:
check_end_tag=''+tag+'>'
start_tag_length=len(check_start_tag)
end_tag_length=len(check_end_tag)
length=len(html)
index=position
start=html.find(check_start_tag, index)
if start>=0:
require=1
while require>0 and (index
index=index+1
if html[index:index+start_tag_length]==check_start_tag:
require=require+1
if html[index:index+end_tag_length]==check_end_tag:
require=require-1
return html[position:index+end_tag_length]
image.gif
这个是从第二篇里的symmetry()方法修改而来, 第2个参数修改为html文本, 增加第3个参数position索引。
qpath()方法修改为match(),语法分析中去除“END”属性标记:
'''
author: MRN6
blog: qq_21264377@blog.csdn.net
updated: Jul. 18th, 2020 Sat. 03:40PM
'''
def match(path=None, html=None):
if path is None or html is None or len(path.strip())<=0 or len(html.strip())<=0:
return []
if not '//' in path:
return []
rules=path.split('//')
matches=[]
submatches=[]
l=len(rules)
c=0
match_html=html
for rule in rules:
c=c+1
if len(rule.strip())<1:
continue
if submatches is not None and len(submatches)>0:
t=submatches
submatches=[]
for submatch in t:
if len(submatch.strip())<=0:
continue
attributecontent=''
if ':' in rule:
ruledatas=rule.split(':')
tag=ruledatas[0]
attributedatas=ruledatas[1].split('=')
attribute=attributedatas[0]
value=attributedatas[1]
attributecontent=attribute+'="'+value+'[^"]*"'
else:
tag=rule
tempmatches=re.findall(']*'+attributecontent, submatch)
if tempmatches is None or tempmatches==[]:
continue
index=0
#print('[match-end]', tempmatches, '[/match-end]')
for tempmatch in tempmatches:
position=submatch.find(tempmatch, index)
while position>=0 and index
match=minimalpair(tag, submatch, position)
index=position+len(match)
if c==l:
matches.append(match)
else:
submatches.append(match)
position=submatch.find(tempmatch, index)
else:
attributecontent=''
attribute=None
value=None
if ':' in rule:
ruledatas=rule.split(':')
tag=ruledatas[0]
attributedatas=ruledatas[1].split('=')
attribute=attributedatas[0]
value=attributedatas[1]
attributecontent=attribute+'="'+value+'[^"]*"'
else:
tag=rule
tempmatches=re.findall(']*'+attributecontent, match_html)
if tempmatches is None or tempmatches==[]:
return []
index=0
#print('[match-root]', tempmatches, '[/match-root]')
for tempmatch in tempmatches:
if not tag in tempmatch or (attribute is not None and value is not None and not attribute+'="'+value+'"' in tempmatch):
continue
position=match_html.find(tempmatch, index)
while position>=0 and index
match=minimalpair(tag, match_html, position)
#print(position, '[match-sub]', match, '[/match-sub]')
index=position+len(match)
if c==l:
matches.append(match)
else:
submatches.append(match)
position=match_html.find(tempmatch, index)
return matches
对path和html进行简单有效性检查后,分析path的语法,得出path的结构。对path进行逐级拆分,并在html内容中进行匹配。首次分析的HTML内容为完整的html。之后每次分析的HTML内容为前一次分析出的语法树submatches。因为每次分析的结果皆采用最小对称法,所以避免重复包含和标签不完整的问题。分析流程抵达最后目标标签时,将结果加入matches中,最后返回matches。
现在用实践来检验一下成果。
https://news.163.com HTML源码
https://news.163.com HTML源码输入路径规则:
mypaths=["//div:class=column_main//h3", "//div:class=column_main//div:class=photo", "//div:class=column_main//ul//li",
"//div:class=bd", "//div:class=bd//div:class=ns_area list", "//div:class=bd//div:class=ns_area list//li",
"//div:class=bd//div:class=ns_area list//ul//li//a", "//div:class=bd//div:class=ns_area list//ul//a",
"//div:class=ntes-quicknav-content//ul//li", "//div:class=ntes-quicknav-content//ul//li//a",
"//div:class=mt35 mod_hot_rank clearfix//ul//li", "//div:class=mt35 mod_hot_rank clearfix//ul//a",
"//div:class=mt35 mod_money//ul//li", "//div:class=mt35 mod_money//div:class=bg//h3",
"//div:class=bottomnews_main clearfix//h2",
"//div:class=ns_area index_media//ul//li//a",
"//meta:http-equiv=Content-Type",
"//meta:name=keywords",
"//title"]
这些都是该HTML文本内容中存在的规则,包含逐级和跳级的。任意选取其一进行测试。
path=mypaths[-10]
results=match(path, html)
print('', path, '')
print('', str(len(results)), '')
counter=0
for result in results:
counter=counter+1
print('['+str(counter)+']', result, '[/'+str(counter)+']')
mypaths[-10]为mypaths数组中倒数第10个,这是python的基本语法。
运行结果:
path=mypaths[-10]运行结果
总结
以上是生活随笔为你收集整理的html 选中tag标签,HTML Tag Selector标签选择器PFC020071801的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 计算机网络技术发源于什么,计算机网络基础
- 下一篇: 计算机专业要学几门课呀,计算机专业学生一