# -*- coding:utf-8 -*- import urllib2 import re url = 'http://www.yahoo.co.jp' word = '社会的責任' def gene(n): s = 100 f = urllib2.urlopen(n) while 1: yield f.read(s) s+=100 def s(): v = gene(url) p = re.compile(word) print "if it dose not find, please enter ctl + c" while 1: a = p.search(v.next()) try: print a.group() break except AttributeError: pass s()
yield statement is return value what it is value on the way. Therefore, I can execute another processing among next(). This function is called delay processing.
Above script is using yield statement and to search word.