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

How to get both following-sibling::text() and following-sibling::b?
Scrapy xpath fail to find certain div in a webpage
Scrapy: Issues in dealing with Abbr tag in Xpath
XPath for ImportXML in Gsheet
XMLStarlet: selecting nodes using less than / greater than
Date range comparison in CQ using XPATH
I don't understand why this XPath expression is not working as a Scrapy selector
Selenium IDE, check (assert) if a dynamic element contains a specific text
xpath help to get buttons under a class where a link contains some href value
How to write xpath for below code displayed on Image
Fetch element child elements in XQuery
Quickly extract value using xpath
WSO2 ESB- Error Handling - On Error Sequence
Multiple xpath expressions
great ancestor & great great ancestor
How do I convert a string to an int variable in BizTalk XLANG?

Categories

HOME
amazon-web-services
mc
svn
keyboard
dotnetnuke
transactions
antd
websocket
cryptography
google-webmaster-tools
wagtail
swarm
watch-os-3
nsstring
phpstorm-2017.1
amazon-elb
texas-instruments
android-contacts
salt-cloud
wsf
spyder
libgit2
device
perfino
pyopencl
fusetools
high-availability
unrar
crud
network-analysis
fatfs
jsonresult
snap-framework
gsp
pdfminer
textmate2
jade4j
forms-authentication
django-crispy-forms
aurelia-cli
variable-variables
elastica
android-n
spring-restcontroller
utf
bit-shift
return-type
nusoap
redisson
extjs4.1
embedded-v8
test-data
kendo-editor
degrees
emf-compare
front-camera
portal
ajaxmin
gnucash
android-async-http
okta-api
enunciate
ui4j
lowercase
dnx50
auto-generate
extend
jnlp
supercomputers
dynamics-ax-2012-r2
karma-coverage
explain
erlog
htmltidy
createfile
string.format
dc
storing-data
nodeload
ivalueconverter
frontbase
stty
google-code-prettify
acceptance-testing
android-authenticator
email-verification
jquery-dialog
struts2-json-plugin
nsmutablestring
telerik-ajax
system-information
yuidoc
page-curl
jdownloader
uninstaller
principles
yahoo-maps
google-translator-toolkit
bucket
projectpier
rakudo

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