在2个关键字之间打印文本

时间:2013-07-31 01:25:28

标签: python html

我正在使用html文件。我想提取第一个第2项之前和第1a项之后的文本(感谢帮助部分)。首先,我删除第二个项目2之后的文本。

text= """"""<this is an example this is Item&nbsp;2. A href="#106">Item&nbsp;1a. thanks for helping <B>Item&nbsp;2. Properties</B> this is an example this is Item&nbsp;2.stachoverflow"""

>>> a=re.search ('(?<=<B>)Item&nbsp;2\.',text)
>>> b = a.span()
>>> newText= text[:b[1]]
>>> c=newText.rfind("1a")
>>> (newText[c[1]:])

TypeError: 'int' object is not subscriptable

如何打印c后面的文字?

1 个答案:

答案 0 :(得分:0)

如果您只是尝试打印输出,则尝试将c作为数组访问 - 它是一个索引。所以要打印c,它只是(newText [c:])。

但是,您的搜索也不正确,因为您需要newText = text [:b [0]],而不是1。

相关问题