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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络技术发源于什么,计算机网络基础
- 下一篇: 计算机专业要学几门课呀,计算机专业学生一