How can Scrapy be used to select the text of an element that has a particular attribute name and value?
For example,
<span property="city">Montreal</span>
I tried the following but received a None
response.css('.span[property="city"]::text').extract_first()
Description. /html/head/title − This will select the <title> element, inside the <head> element of an HTML document. /html/head/title/text() − This will select the text within the same <title> element. //td − This will select all the elements from <td>.
When you are using text nodes in a XPath string function, then use . (dot) instead of using .//text(), because this produces the collection of text elements called as node-set.
We are using response. css() to select all the elements with the class title and the tag a. Then we are using the ::attr(href) to select the href attribute of all the elements we have selected. Then we are using the getall() to get all the values of the href attribute.
You are making a small mistake. You need to drop the '.'
before 'span'
:
In [6]: response.css('span[property="city"]::text').extract_first()
Out[6]: u'Montreal'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With