Issue
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.
Solution
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()
Check this about xpath injection
Answered By - Gilles Quenot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.