您的位置:首頁技術文章
文章詳情頁

python - 正則表達式匹配html的問題。

瀏覽:73日期:2022-09-05 14:29:29

問題描述

<dd class='gray6'> <span class='gray6'> 中文 <span class='padl27'></span> 中文 </span> 中文內容 #需要抓取的內容</dd>用BeautifulSoup html.parser解析的網頁,現在用re模塊想抓取**第7行**的中文內容,放在一個組里面(.*?)。正則老是匹配不上,用換行符也匹配不上,不知道怎么寫了。。。

問題解答

回答1:

既然你都用bs4解析了,為什么不用它提取哪?bs4內有一個stripped_string的函數正好滿足你的需要。

回答2:

import repattern = re.compile(r’</span>.*?</span>(.*?)</dd>’, re.S)str = ’’’<dd class='gray6'> <span class='gray6'> 中文 <span class='padl27'></span> 中文 </span> 中文內容 #需要抓取的內容</dd>’’’print(pattern.search(str).group(1))===> 中文內容 #需要抓取的內容回答3:

const re = /^</span>(.*)</dd>$/

這樣可以不?

標簽: Python 編程
相關文章:
国产综合久久一区二区三区