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
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.
>