Python Scrapy: Skip Xpath if it's not there
<p>I have this code which scrapes a few hundred pages for me. But sometimes the xpath for <code>a</code> doesn't exist at all, how can I edit this so the script doesn't stop and keeps running to get the <code>b</code> and just give me that for that specific page?</p>

<pre><code>`a = response.xpath("//div[@class='headerDiv']/a/@title").extract()[0]
b = response.xpath("//div[@class='headerDiv']/text()").extract()[0].strip()
items['title'] = a + " " + b
yield items`
</code></pre>

<p>Just check the result of <code>extract()</code>.</p>

<pre><code>nodes = response.xpath("//div[@class='headerDiv']/a/@title").extract()
a = nodes[0] if nodes else ""

nodes = response.xpath("//div[@class='headerDiv']/text()").extract()
b = nodes[0].strip() if nodes else ""

items['title'] = a + " " + b
yield items
</code></pre>

<p>With the good advice of Padraic Cunningham:</p>

<pre><code>a = response.xpath("//div[@class='headerDiv']/a/@title").extract_first(default='')
b = response.xpath("//div[@class='headerDiv']/text()").extract_first(default ='').strip() 
items['title'] = (a + " " + b).strip()
yield items
</code></pre>

python xpath scrapy