python - 正則表達式匹配html的問題。
問題描述
<dd class='gray6'> <span class='gray6'> 中文 <span class='padl27'></span> 中文 </span> 中文內(nèi)容 #需要抓取的內(nèi)容</dd>用BeautifulSoup html.parser解析的網(wǎng)頁,現(xiàn)在用re模塊想抓取**第7行**的中文內(nèi)容,放在一個組里面(.*?)。正則老是匹配不上,用換行符也匹配不上,不知道怎么寫了。。。
問題解答
回答1:既然你都用bs4解析了,為什么不用它提取哪?bs4內(nèi)有一個stripped_string的函數(shù)正好滿足你的需要。
回答2:import repattern = re.compile(r’</span>.*?</span>(.*?)</dd>’, re.S)str = ’’’<dd class='gray6'> <span class='gray6'> 中文 <span class='padl27'></span> 中文 </span> 中文內(nèi)容 #需要抓取的內(nèi)容</dd>’’’print(pattern.search(str).group(1))===> 中文內(nèi)容 #需要抓取的內(nèi)容回答3:
const re = /^</span>(.*)</dd>$/
這樣可以不?
相關(guān)文章:
1. docker鏡像push報錯2. javascript - 如何獲取未來元素的父元素在頁面中所有相同元素中是第幾個?3. 關(guān)于docker下的nginx壓力測試4. linux運維 - python遠程控制windows如何實現(xiàn)5. node.js - node express 中ajax post請求參數(shù)接收不到?6. java - tomcat服務(wù)經(jīng)常晚上會掛,求解?7. python - django models 為生成的html元素添加樣式。8. java - 原生CGLib內(nèi)部方法互相調(diào)用時可以代理,但基于CGLib的Spring AOP卻代理失效,為什么?9. html5 - 怎么用npm下載react3版本的路由呢。10. javascript - js判斷一個數(shù)組是否重復
