python - 要抓取的網(wǎng)頁內(nèi)容在括號(hào)里,正則怎么寫?
問題描述
<dt> <span class='gray6 '>名稱:</span> <a target='_blank'>安現(xiàn)</a> ( <a href='http://www.lshqa.cn/ho-a021551/' target='_blank'> 中間 </a><a href='http://www.lshqa.cn/ho-a011551-b03497/' target='_blank'> 草原牧場(chǎng) #這是我要抓取的內(nèi)容我把它放到組(.*?)里面。 </a> ) [ <a href='http://www.lshqa.cn/wenda/acchor'>圖文</a> ] </dt>
我就是想抓取第9行那里的中文內(nèi)容,放到一個(gè)組里面。本來應(yīng)該很簡(jiǎn)單,但是試了好多次就是抓不到,報(bào)錯(cuò)是沒有內(nèi)容。估計(jì)和第5行,和11行的括號(hào)有關(guān)。請(qǐng)問怎么寫正則才能抓得到?我原來這樣寫
d = re.search(r’<a id='agatdesf_B02_10'.*?>s*(.*?)s*</a>’, s).group(1)
抓不到,誰幫我看下錯(cuò)在哪里?
問題解答
回答1:python3
html=’’’ <dt> <span class='gray6 '>名稱:</span> <a target='_blank'>安現(xiàn)</a> ( <a href='http://www.lshqa.cn/ho-a021551/' target='_blank'> 中間 </a><a href='http://www.lshqa.cn/ho-a011551-b03497/' target='_blank'> 草原牧場(chǎng) #這是我要抓取的內(nèi)容我把它放到組(.*?)里面。 </a> ) [ <a href='http://www.lshqa.cn/wenda/acchor'>圖文</a> ] </dt>’’’import rep=re.compile(r’<as+?[^>]*?id='agatdesf_B02_10'[^>]*>([^<]*)</a>’)print(p.findall(html)[0].strip())##草原牧場(chǎng) #這是我要抓取的內(nèi)容我把它放到組(.*?)里面。回答2:
推薦一本書 精通正則表達(dá)式 看看就都會(huì)了!
