成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python - 要抓取的網(wǎng)頁(yè)內(nèi)容在括號(hào)里,正則怎么寫(xiě)?

瀏覽:133日期:2022-09-02 08:56:00

問(wèn)題描述

<dt> <span class='gray6 '>名稱:</span> <a target='_blank'>安現(xiàn)</a> ( <a href='http://m.piao2010.com/ho-a021551/' target='_blank'> 中間 </a><a href='http://m.piao2010.com/ho-a011551-b03497/' target='_blank'> 草原牧場(chǎng) #這是我要抓取的內(nèi)容我把它放到組(.*?)里面。 </a> ) [ <a href='http://m.piao2010.com/wenda/acchor'>圖文</a> ] </dt>

我就是想抓取第9行那里的中文內(nèi)容,放到一個(gè)組里面。本來(lái)應(yīng)該很簡(jiǎn)單,但是試了好多次就是抓不到,報(bào)錯(cuò)是沒(méi)有內(nèi)容。估計(jì)和第5行,和11行的括號(hào)有關(guān)。請(qǐng)問(wèn)怎么寫(xiě)正則才能抓得到?我原來(lái)這樣寫(xiě)

d = re.search(r’<a id='agatdesf_B02_10'.*?>s*(.*?)s*</a>’, s).group(1)

抓不到,誰(shuí)幫我看下錯(cuò)在哪里?

問(wèn)題解答

回答1:

python3

html=’’’ <dt> <span class='gray6 '>名稱:</span> <a target='_blank'>安現(xiàn)</a> ( <a href='http://m.piao2010.com/ho-a021551/' target='_blank'> 中間 </a><a href='http://m.piao2010.com/ho-a011551-b03497/' target='_blank'> 草原牧場(chǎng) #這是我要抓取的內(nèi)容我把它放到組(.*?)里面。 </a> ) [ <a href='http://m.piao2010.com/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:

推薦一本書(shū) 精通正則表達(dá)式 看看就都會(huì)了!

標(biāo)簽: Python 編程