xpath


Extract first element with XPath and scrapy


I use .extract() to get the data from a xpath, like:
response.xpath('//*#id="bakery"]/span[2]/text()').extract()
the issue with this is that I always get a list as response.
for example:
['23']
I only want the number, so I try with:
response.xpath('//*#id="bakery"]/span[2]/text()').extract()[0]
but this is a problem is the list empty, although I can use an exception to handle that scenario I guess there is a better way to do it
.extract_first() to the rescue:
response.xpath('//*#id="bakery"]/span[2]/text()').extract_first()
Instead of an exception, it would return None if no elements were matched.

Related Links

multiple string() results for an xpath?
XQuery/XPath: Using count() and max() function for return of element with highest count
Double iteration over a nodeset in XSLT-1.0
What is the correct XPath query for 'select onchange'
XPath 2.0: Retrieving nodes by attribute where value is case Insensitive
XPath - abbreviation of position() function
XPath Query in JMeter
xpath expression to select attribute value
DOMDocument xpath query cut off time
How to parse the following html code get all text before “br” tag
XPath selector by class AND index
How to perform set operations in XPath 1.0
extract data from a div that have no class using xpath
Xpath: Select node but not specific child elements
XQuery - How to use eq (=) on a group of values
better selenium xpath is expecting

Categories

HOME
oracle
seedstack
transactions
telegraf
scheduler
wine
xades4j
nexus3
locationmanager
nsstring
feature-extraction
maven-plugin
cpu-usage
parceler
node-webkit
kaggle
amazon-sns
device
candlestick-chart
beego
tortoisehg
django-import-export
adapter
bonita
tsung
d3.js-v4
fotorama
lxml
counting
payeezy
des
redgate
bluemixtools
ftp-server
nsuserdefaults
cargo
mars-simulator
tidal-scheduler
julius-speech
permission-denied
gcal
sonarqube5.2
directwrite
django-static-precompiler
brightscript
knockout-2.0
entropy
solr-query-syntax
iptv
spring.net
ptvs
prefetch
bpms
yargs
portal
nsrunloop
android-4.2-jelly-bean
meld
preferenceactivity
univocity
teensy
openshift-cartridge
dsa
green-threads
artisan
deque
innerhtml
maven-shade-plugin
nanomsg
google-hangouts
datagridcomboboxcolumn
xhprof
senchatouch-2.4
forceclose
oxygene
jms-serializer
ctp
compiler-flags
eclim
node.js-stream
statechart
blobstorage
session-0-isolation
live-wallpaper
java.lang.class
liveconnect
vows
fork-join
servlet-container
gaelyk
suphp
android-2.1-eclair
3-tier
j2mepolish
zend-test
firephp
eai
objectinstantiation
chronic

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App