Passing arguments to callback functions

1,509 views
Skip to first unread message

tarasm

unread,
Mar 16, 2009, 10:29:14 PM3/16/09
to scrapy-users
Hello,
I'm encountering a weird problem that I can't seem to overcome.

I'm trying to pass a variable to a callback function.

when I do

Request(url, callback=self.testfunction)

def testfunction(self, response):
// here i have access to the response

but if I try to pass an argument to testfunction in the following way:

Request(url, callback=self.testfunction(title))

def testfunction(self, test):
// I get the test argument, but I loose the response

What do I do?
Thank you,
Taras

Patrick Mézard

unread,
Mar 17, 2009, 3:44:36 AM3/17/09
to scrapy...@googlegroups.com
tarasm a écrit :


callback = lambda response: self.testfunction(response, test)
Request(url, callback=callback)

[...]

def testfunction(self, response, test):
pass

--
Patrick Mézard

tarasm

unread,
Mar 18, 2009, 1:20:58 AM3/18/09
to scrapy-users
Hello Patrick,

That worked perfectly.
It would be great if this was in the documentation. It would have
saved me so much time.

Thank you again,
Taras

Patrick Mézard

unread,
Mar 18, 2009, 3:49:29 AM3/18/09
to scrapy...@googlegroups.com
tarasm a écrit :

> Hello Patrick,
>
> That worked perfectly.
> It would be great if this was in the documentation. It would have
> saved me so much time.

Well, this is a python question, completely unrelated to scrapy library.

http://www.faqts.com/knowledge_base/view.phtml/aid/6081/fid/241

--
Patrick Mézard

tarasm

unread,
Mar 19, 2009, 9:19:33 AM3/19/09
to scrapy-users
Well,

I hope scrapy guys will take my suggestion under consideration,
considering that this directly applies to writing scrapy spiders.

Thank you Patrick,
Taras

Pablo Hoffman

unread,
Mar 22, 2009, 12:32:08 PM3/22/09
to scrapy...@googlegroups.com
Taras,

I've followed your suggestion and added documentation about passing
arguments to request callbacks (in r1005 & r1006).

Here's the link:
http://doc.scrapy.org/ref/request-response.html#passing-arguments-to-callback-functions

Regards,
Pablo.
Reply all
Reply to author
Forward
0 new messages