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>You can use as follow:</p>

<pre><code>import lxml.etree as etree

parser = etree.XMLParser(strip_cdata=False, remove_comments=True)
root = etree.fromstring(data, parser)

#Take Hyperlink as per xpath:
#But Xpath returns list of element so we have to take 0 index of it if it has element

a = root.xpath("//div[@class='headerDiv']/a/@title")
b = response.xpath("//div[@class='headerDiv']/text()")

if a:
    items['title'] = a[0].strip() + " " + b[0].strip() 
else:
    items['title'] = b[0].strip() 

yield items
</code></pre>

python xpath scrapy