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)容,放在一個組里面(.*?)。正則老是匹配不上,用換行符也匹配不上,不知道怎么寫了。。。
問題解答
回答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. 怎么在網(wǎng)頁中設(shè)置圖片進(jìn)行左右滑動2. node.js - mysql如何通過knex查詢今天和七天內(nèi)的匯總數(shù)據(jù)3. mysql 插入數(shù)值到特定的列一直失敗4. mysql 怎么做到update只更新一行數(shù)據(jù)?5. python2.7 - python 函數(shù)或者類 代碼的執(zhí)行順序6. javascript - 新浪微博網(wǎng)頁版的字?jǐn)?shù)限制是怎么做的7. python - 多態(tài)調(diào)用方法時卻顯示bound method...8. 360瀏覽器與IE瀏覽器有何區(qū)別???9. javascript - 用jsonp抓取qq音樂總是說回調(diào)函數(shù)沒有定義10. sublime可以用其他編譯器替換嗎?
