2

I want to grab the cover of the book on this HTML. The cover sometimes is at the first order, sometimes it is at the second order.

<div class="content">
    <ul>
        <li>
            <b>Series</b>
            Campell
        </li>
        <li>
            <b>Hardcover:</b>
            1465 pages
        </li>
    </ul>
</div>

I put the cover types in this list

cover = ['Hardcover', 'BoardBook', 'CardBook']

When I specifically write 'Hardcover' in the xpath, it works.

response.xpath("//li/b[contains(text(),'Hardcover')]/text()").extract()

However, when I use the index of the list cover[0], it brings other things as well.

response.xpath('//li/b[contains(text(),cover[0])]/text()').extract()

I want to iterate the list values to check one of them between tags.

Land Owner
  • 182
  • 11
  • 1
    Duplicate of [How to use Python variable in an XPath expression?](https://stackoverflow.com/questions/37323317/how-to-use-python-variable-in-an-xpath-expression) and [How to pass variable parameter into XPath expression?](https://stackoverflow.com/questions/30352671/how-to-pass-variable-parameter-into-xpath-expression) – kjhughes Mar 30 '18 at 11:32

1 Answers1

3

You need string concatenation :

response.xpath('//li/b[contains(text(), "' + cover[0] + '")]/text()').extract()

or

xpath_string = '//li/b[contains(text(), "{}")]/text()'.format(cover[0])
response.xpath(xpath_string).extract()

warning Check this about xpath injection

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223