初识python yield

Posted 教程之父

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识python yield相关的知识,希望对你有一定的参考价值。

for sel in response.xpath(‘//ul/li‘):
item = DmozItem()
item[‘title‘] = sel.xpath(‘a/text()‘).extract()
item[‘link‘] = sel.xpath(‘a/@href‘).extract()
item[‘desc‘] = sel.xpath(‘text()‘).extract()
yield item


等价于:


items []
for sel in response.xpath(‘//ul/li‘):
item = DmozItem()
item[‘title‘] = sel.xpath(‘a/text()‘).extract()
item[‘link‘] = sel.xpath(‘a/@href‘).extract()
item[‘desc‘] = sel.xpath(‘text()‘).extract()
   items.append(item)
return items

就这么简单!

以上是关于初识python yield的主要内容,如果未能解决你的问题,请参考以下文章