Reputation: 205
Below is the html.
<div class="list-items tab-pane fade active show" ng-class="type=='pay'?'show active':''" tabindex="1" id="pay-bills">
<div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">
<h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>
</div>
<div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">
<h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>
</div>
</div>
I want to get the second h2 value i.e. "Gas Bill Payment". I wrote xpath as
//div[@id='pay-bills']//div//h2[@class='head ng-binding'][2]
The issue is I am unable to get the second value with "[2]". although when i insert "[1]" i get two values.
Upvotes: 0
Views: 120
Reputation: 5905
Why this doesn't work :
//div[@id='pay-bills']//div//h2[@class='head ng-binding'][2]
You're looking for an h2
element whith a position equals to 2. All h2
elements in your sample data are in first (1) position.
You can use the solution proposed by @chrisis or could go with :
(//div[@id='pay-bills']/div/h2)[2]/text()
Use ()
to specify you want the second displayed result of the XPath enclosed by ()
.
Be sure also to remove the unnecessary //
from your expression (should be /
since the second div
and h2
are child elements).
Upvotes: 2
Reputation: 1993
You want the second div, what you've tried would give you the second h2 inside the div if there was one. Try
//div[@id='pay-bills']//div[2]//h2[@class='head ng-binding']
Upvotes: 0
Reputation: 2609
Hope it may help.
>>> html = """ <div class="list-items tab-pane fade active show" ng-class="type=='pay'?'show active':''" tabindex="1" id="pay-bills"
... >
...
... <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">
... <h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>
... </div>
...
... <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">
... <h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>
... </div>
...
... </div>"""
>>> html
' <div class="list-items tab-pane fade active show" ng-class="type==\'pay\'?\'show active\':\'\'" tabindex="1" id="pay-bills">\n\n <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>\n </div>\n\n <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>\n </div>\n\n</div>'
>>> from pprint import pprint
>>> pprint(html)
(' <div class="list-items tab-pane fade active show" '
'ng-class="type==\'pay\'?\'show active\':\'\'" tabindex="1" id="pay-bills">\n'
'\n'
' <div ng-repeat="item in BillList.Result.categorizedBillNickBene" '
'class="ng-scope">\n'
' <h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity '
'Bill Payment</h2>\n'
' </div>\n'
'\n'
' <div ng-repeat="item in BillList.Result.categorizedBillNickBene" '
'class="ng-scope">\n'
' <h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill '
'Payment</h2>\n'
' </div>\n'
'\n'
'</div>')
>>> from scrapy.http import HtmlResponse
>>> response = HtmlResponse(url='my html string', body=html, encoding='utf-8')
>>> response.xpath('//div')
[<Selector xpath='//div' data='<div class="list-items tab-pane fade ...'>, <Selector xpath='//div' data='<div ng-repeat="item in BillList.Resu...'>, <Selector xpath='//div' data='<div ng-repeat="item in BillList.Resu...'>]
>>> response.xpath('//div').getall()
['<div class="list-items tab-pane fade active show" ng-class="type==\'pay\'?\'show active\':\'\'" tabindex="1" id="pay-bills">\n\n <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>\n </div>\n\n <div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>\n </div>\n\n</div>', '<div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>\n </div>', '<div ng-repeat="item in BillList.Result.categorizedBillNickBene" class="ng-scope">\n <h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>\n </div>']
>>> response.xpath('//div//h2').getall()
['<h2 class="head ng-binding" ng-show="filtered.length!=0">Electricity Bill Payment</h2>', '<h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>']
>>> response.xpath('//div//h2').getall()[1]
'<h2 class="head ng-binding" ng-show="filtered.length!=0">Gas Bill Payment</h2>'
>>> response.xpath('//div//h2/text()').getall()[1]
'Gas Bill Payment'
Upvotes: 0