日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

html 选中tag标签,HTML Tag Selector标签选择器PFC020071801

發布時間:2024/9/15 HTML 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基本要求標簽對稱。這樣就可以忽略正則表達式貪婪匹配帶來的“多重包含問題”--標簽不完整、包含多個匹配結果??匆韵麓a最小對稱法 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。