how to pass python variable to xpath expression?

1,928 views
Skip to first unread message

Ryan Chun Pui Keong

unread,
Dec 11, 2011, 10:38:18 AM12/11/11
to scrapy-users
Dear All,

I have a numeric variable called "index". How can pass it to xpath
expression?

For example, I would like to create an xpath expression like below.
But it doesn't work. Can anyone shed some light? Thanks.

hxs.select('div[@class="new"][$index]').extract()

BR,
Ryan

Rolando Espinoza La Fuente

unread,
Dec 11, 2011, 7:23:48 PM12/11/11
to scrapy...@googlegroups.com
On Sun, Dec 11, 2011 at 11:38 AM, Ryan Chun Pui Keong
<puik...@gmail.com> wrote:
> Dear All,
>
> I have a numeric variable called "index". How can pass it to xpath
> expression?

Is that "index" variable a python variable?

If so, you can use usual python's string interpolation:

xpath = "//foo/bar[%s]" % index

xpath = "//foo/bar[{0}]".format(index)
xpath = "//foo/bar[{index}]".format(index=index)

results = hxs.select(xpath)

The .format() statements are supported in python 2.6+.

Regards,

~Rolando

> For example, I would like to create an xpath expression like below.
> But it doesn't work. Can anyone shed some light? Thanks.
>
> hxs.select('div[@class="new"][$index]').extract()
>
> BR,
> Ryan
>

> --
> You received this message because you are subscribed to the Google Groups "scrapy-users" group.
> To post to this group, send email to scrapy...@googlegroups.com.
> To unsubscribe from this group, send email to scrapy-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/scrapy-users?hl=en.
>

Tomas Neme

unread,
Dec 12, 2011, 9:27:20 AM12/12/11
to scrapy...@googlegroups.com
you can also do this:

xpath = "//foo/bar[%d]"

for index in range(some_number):
    results = hxs.select(xpath % index)
--
"The whole of Japan is pure invention. There is no such country, there are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny 
(")_(") to help him gain world domination.

Ryan Chun Pui Keong

unread,
Dec 14, 2011, 11:31:01 AM12/14/11
to scrapy-users
Hi Guys, thanks so much for your help!
Reply all
Reply to author
Forward
0 new messages