Python的yield和return语句?和 Scrapy 产生请求
举例说明yield
和return
有什么区别? 当我们在生成器中产生任何值或请求时,实际上会发生什么?
我没有从任何其他函数或程序调用我的生成器。
我的循环是:
for index in range(3):
yield Request(url,callback=parse)
这是对特定网址发出请求并在请求后调用回调函数。这段代码是做什么的?
代码后面的顺序是什么?
What is the difference between yield
and return
explain with example?
and what actually happens when in the generator we yield
any value or request?
I'm not calling my generator from any other function or program.
My loop is:
for index in range(3):
yield Request(url,callback=parse)
This is making requests on the specific url and calling the callback function after the request. What this code is doing?
And what is the sequence followed by the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您在函数
start_requests()
中遇到了这个难题,其中包含上下文yield
。例如:
当你引用scrapy的文档 spider 然后找到名为
start_requests()
的函数,它表示该方法必须返回一个可迭代对象。如果你将yield更改为return,它就不是一个可迭代的,因为当你启动你的spider时,for循环已经结束了。这可能会变得一团糟。很自然,您的蜘蛛应该将 http 请求一一发送到这些目的地,因此最好的方法是生成器。在 for 循环中,您的蜘蛛将在
yield
处停止并返回scrapy.Request()
,完成所有操作后,您的蜘蛛将send()
> 到生成器并继续下一步列表中的以下网址。
I guess you are faced with the puzzle in the function
start_requests()
with the contextyield
in it.For example:
When you refer to the document of scrapy spider and then find the function named
start_requests()
,it says the method must return an iterable. If you change yield to return, it is not an iterable because the for loop is already over when you start your spider.It could be a mess.It is natural that your spider should send http requests to these destinations one by one so the best way is a generator. In the for loop, your spider will stop at
yield
and returnscrapy.Request()
, with all things done, your spider willsend()
to generator and move on to nextfollowing urls in the list.
@Jochen 链接的问题没有回答您的问题的唯一方面是“我没有从任何其他函数或程序调用我的生成器。”。
您定义爬虫类,然后 scrapy 调用您定义的(特殊)函数,如文档中指定的那样。 (例如
parse
函数是未指定回调的请求的默认回调)。The only aspect of your question that isn't answered by the question linked to by @Jochen is "i am not calling my generator from any other function or program.".
You define your crawler class, and scrapy calls the (special) functions you define, as specified in the documentation. (For example the
parse
function is the default callback for requests that don't specify a callback).