Xodarap777
Xodarap777

Reputation: 1376

Xpath to get text following label

I want to get items according to their (preceding) <label> attributes, like this:

 <div>
      <ul>
          <li class="phone">
               <label>Mobile</label>
               312-999-0000
 <div>
      <ul>
          <li class="phone">
               <label>Home</label>
               312-999-0001

I want to put the first number in the "Mobile" column/list, and the second in the Home list. I currently have code grabbing both of them, but I don't know the proper syntax for getting the label as it is in the source. This is what I'm using now:

for target in targets:
    item = CrawlerItem()
    item['phonenumbers'] = target.xpath('div/ul/li[@class="phone"]/text()').extract()

How should I rewrite that for item['mobilephone'] and item['homephone'], using the labels?

Upvotes: 2

Views: 1922

Answers (1)

Xodarap777
Xodarap777

Reputation: 1376

I found the answer while finishing up the question, and thought I should share it:

item['mobilephone'] = target.xpath('div/ul/li/label[contains (text(),"Mobile")]/following-sibling::text()').extract()
item['officephone']= target.xpath('div/ul/li/label[contains (text(),"Office")]/following-sibling::text()').extract()

Upvotes: 2

Related Questions