Shawn Rong
Shawn Rong

Reputation: 145

Python Xpath how to check if a element exist?

I'm writing a simple python web crawler.I try to use the Xpath to filter the page.Here's part of target page.

<p class="mt12">21 
  <span class="line">|</span>low 18
  <span class="line">|</span>north
  <span class="line">|</span>2016
</p>

 <p class="mt12">22
  <span class="line">|</span>low 19
  <span class="line">|</span>2018
</p>

Some items have three description and some have four.How to use Xpath to check whether element exist?I want to extract all of the descriptions like:

# if element four exists get span four
good['year'] = goods.xpath("p[@class='mt12']/text()[4]")
# else get span three
good['year'] = goods.xpath("p[@class='mt12']/text()[3]")

Upvotes: 1

Views: 2693

Answers (1)

Andersson
Andersson

Reputation: 52665

Try to use below code:

good['year'] = goods.xpath("p[@class='mt12']/text()[4]") or goods.xpath("p[@class='mt12']/text()[3]")

It should return you text()[4] if it's exists (not empty string) or text()[3] otherwise

Update

In case "p[@class='mt12']/text()[4]" expression returns you exception, you can apply try/except block as below:

try:
    good['year'] = goods.xpath("p[@class='mt12']/text()[4]")
except IndexError:
    good['year'] = goods.xpath("p[@class='mt12']/text()[3]")

Upvotes: 1

Related Questions