代码之家  ›  专栏  ›  技术社区  ›  Prashant

如何仅刮取特定单词

  •  1
  • Prashant  · 技术社区  · 9 年前

    安得拉邦

    >>> container.findAll('b')
    [<b><lable style="color:#3097b0;"> Aganampudi ( Public Funded ) </lable></b>, <b>NH-16 in Andhra Pradesh <br/> Stretch : </b>, <b>Tollable Length :</b>, <b>Fee Effective Date : </b>, <b>  Due date of toll revision : </b>, <b style="color:Orange"> (With Discounting) </b>, <b> Rest Areas : </b>, <b>Truck Lay byes :</b>, <b>Static Weigh Bridge : </b>, <b> Helpline No. : </b>, <b>Emergency Services :</b>, <b>Nearest Police Station: </b>, <b>Highway Administrator (Project Director): </b>, <b>Project Implementation Unit(PIU)</b>, <b>Regional Office(RO)</b>, <b>Representative of Consultant</b>, <b>Representative of Concessionaire: </b>, <b>Nearest Hospital(s): </b>]
    >>> search1 = container.findAll('b')
    >>> search1[1]
    <b>NH-16 in Andhra Pradesh <br/> Stretch : </b>
    >>>
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   chad    9 年前

    您可以使用Python的字符串函数来提取它。

    我只是查找索引“in”是6,“Stretch”是25,使用 .index() text[onset + 2:offset] -这是Python版本的子字符串。如果你需要澄清,请告诉我。

    text = search1[1].get_text()
    onset = text.index('in')
    offset = text.index('Stretch')
    name = str(text[onset + 2:offset]).strip(' ')
    print name