gpt4 book ai didi

python - 如何仅抓取特定单词

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:56 27 4
gpt4 key购买 nike

我只想抓取单词Andhra Pradesh,我正在努力为此编写查询,任何帮助将不胜感激。

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

最佳答案

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

清理后的字符串将如下所示“NH-16 in Andhra Pradesh Stretch :”

我只是使用 .index() 查找“in”的索引(6)和“Stretch”的索引(25),然后使用 获取索引 6 到 25 的文本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

关于python - 如何仅抓取特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46032201/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com