Given this html:
- This is a link
- This is another link
XPath generally cannot select what is not there. These things do not exist in your HTML:
[
'This is a link',
'This is another link.'
]
They might exist conceptually on the higher abstraction level that is the browser's rendering of the source code, but strictly speaking even there they are separate, for example in color and functionality.
On the DOM level there are only separate text nodes and that's all XPath can pick up for you.
Therefore you have three options.
text() nodes and join their individual values in Python code. elements and for each of them, evaluate string(.) or normalize-space(.) with Scrapy. normalize-space() would deal with whitespace the way you would expect it. elements and access their .text property – which internally finds all descendant text nodes and joins them for you.Personally I would go for the latter with //ul/li as my basic XPath expression as this would result in a cleaner solution.
As @paul points out in the comments, Scrapy offers a nice fluent interface to do multiple processing steps in one line of code. The following code implements variant #2:
selector = scrapy.Selector(text='''
- This is a link
- This is another link.
''')
selector.css('ul > li').xpath('normalize-space()').extract()
# --> [u'This is a link', u'This is another link.']