python - 正則表達(dá)式匹配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)容,放在一個(gè)組里面(.*?)。正則老是匹配不上,用換行符也匹配不上,不知道怎么寫了。。。
問題解答
回答1:既然你都用bs4解析了,為什么不用它提取哪?bs4內(nèi)有一個(gè)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不顯示端口映射呢?2. angular.js - 關(guān)于$apply()3. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””4. macos - mac下docker如何設(shè)置代理5. MySQL數(shù)據(jù)庫中文亂碼的原因6. docker - 各位電腦上有多少個(gè)容器???容器一多,自己都搞混了,咋辦呢?7. docker gitlab 如何git clone?8. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?9. css - C#與java開發(fā)Windows程序哪個(gè)好?10. docker-compose 為何找不到配置文件?
