2013-07-31 26 views
0

我正在處理html文件。我想提取在第一個項目  2之前和項目  1a之前出現的文本(感謝幫助部分)。首先,我刪除第二項後到來的文本  2.在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之後的文本?

回答

0

如果你只是試圖打印輸出,你試圖訪問c作爲一個數組 - 它是一個索引。所以要打印c,它只會是(newText [c:])。

但是,由於您需要newText = text [:b [0]]而不是1,因此您的搜索不正確。

相關問題