Maison  >  Questions et réponses  >  le corps du texte

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

    <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)  

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

巴扎黑巴扎黑2741 Il y a quelques jours538

répondre à tous(2)je répondrai

  • PHP中文网

    PHP中文网2017-04-18 10:27:03

    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 re
    p=re.compile(r'<a\s+?[^>]*?id="agatdesf_B02_10"[^>]*>([^<]*)</a>')
    print(p.findall(html)[0].strip())
    ##草原牧场  #这是我要抓取的内容我把它放到组(.*?)里面。

    répondre
    0
  • PHPz

    PHPz2017-04-18 10:27:03

    Recommandez un livre pour maîtriser les expressions régulières. Lisez-le et vous le saurez !

    répondre
    0
  • Annulerrépondre