您的位置:首页技术文章
文章详情页

python - 要抓取的网页内容在括号里,正则怎么写?

【字号: 日期:2022-09-02 08:56:00浏览:45作者:猪猪

问题描述

<dt> <span class='gray6 '>名称:</span> <a href='http://xiandai007.com/esf/' target='_blank'>安现</a> ( <a href='https://www.haobala.com/ho-a021551/' target='_blank'> 中间 </a><a href='https://www.haobala.com/ho-a011551-b03497/' target='_blank'> 草原牧场 #这是我要抓取的内容我把它放到组(.*?)里面。 </a> ) [ <a href='https://www.haobala.com/wenda/acchor'>图文</a> ] </dt>

我就是想抓取第9行那里的中文内容,放到一个组里面。本来应该很简单,但是试了好多次就是抓不到,报错是没有内容。估计和第5行,和11行的括号有关。请问怎么写正则才能抓得到?我原来这样写

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

抓不到,谁帮我看下错在哪里?

问题解答

回答1:

python3

html=’’’ <dt> <span class='gray6 '>名称:</span> <a href='http://xiandai007.com/esf/' target='_blank'>安现</a> ( <a href='https://www.haobala.com/ho-a021551/' target='_blank'> 中间 </a><a href='https://www.haobala.com/ho-a011551-b03497/' target='_blank'> 草原牧场 #这是我要抓取的内容我把它放到组(.*?)里面。 </a> ) [ <a href='https://www.haobala.com/wenda/acchor'>图文</a> ] </dt>’’’import rep=re.compile(r’<as+?[^>]*?id='agatdesf_B02_10'[^>]*>([^<]*)</a>’)print(p.findall(html)[0].strip())##草原牧场 #这是我要抓取的内容我把它放到组(.*?)里面。回答2:

推荐一本书 精通正则表达式 看看就都会了!

标签: Python 编程