目标提取内容在网页的()中,re正则需要怎么来写能匹配到?,提取re,


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

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

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

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

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

python3

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

编橙之家文章,

评论关闭