2013-03-14 27 views
1

我試圖從html文本中搜索一個單詞(即。meta),並從該單詞的位置開始打印以下20個字符。Python - 從位置開始打印文本(html)

下面的代碼不返回任何內容:

import os,sys,urllib.request 
url = "http://www.google.com/" 
req = urllib.request.Request(url) 
response = urllib.request.urlopen(req) 
html = response.read() 
html2 = html.decode("windows-1252") 
b2='meta' 
position=html2.index(b2) 
if b2 in html2: 
    print(html2[position:20]) 

回答

0

只要改變你的代碼:

print(html2[position: position + 20]) 
+0

謝謝!沒有看到它 – user2169081 2013-03-14 09:43:38

+0

@ user2169081歡迎您))) – 2013-03-14 09:44:45

0

在最後一行嘗試print(html2[position : position + 20])