import requests import re url = "http://www.acfun.tv/v/list110/index.htm?from=fixedNav" context = requests.get(url).text pattern2 = re.compile(‘<div class="desc">(.*?)</div>‘) pattern3 = re.compile(‘class="title">(.*?)</a><p class="article-info">‘) text2 = pattern2.findall(context) text3 = pattern3.findall(context) def show2(): n = 0 for text in text2: n = yield n if not n: return print(n) print(text) def show3(s): s.send(None) n = 0 for text in text3: n = n+1 print(n) print(text) s.send(n) s.close() s = show2() show3(s)
def show3(s): s.send(None) n = 0 m = 0 for i in text3: m = m+1 for text in text3: n = n+1 print(n) print(text) if n<m: s.send(n) s.close()
虽然避免了报错,但是show2少执行了一次
换种思路,show2执行次数由show3控制,show改为while true循环
import requests import re url = "http://www.acfun.tv/v/list110/index.htm?from=fixedNav" context = requests.get(url).text pattern2 = re.compile(‘<div class="desc">(.*?)</div>‘) pattern3 = re.compile(‘class="title">(.*?)</a><p class="article-info">‘) text2 = pattern2.findall(context) text3 = pattern3.findall(context) def show2(): n = 0 text = (x for x in text2) while True: n = yield if not n: return print(n) print(next(text)) def show3(s): s.send(None) n = 0 for text in text3: n = n+1 print(n) print(text) s.send(n) s.close() s = show2() show3(s)
原文:http://www.cnblogs.com/linxingyi/p/5123097.html