Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrapy .css select element with a specific attribute name and value

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() 
like image 613
Nyxynyx Avatar asked Oct 02 '16 04:10

Nyxynyx


People also ask

How do I extract text from Scrapy?

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>.

How do you write XPath for Scrapy?

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.

How do you make a href in Scrapy?

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.


1 Answers

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'
like image 90
Nehal J Wani Avatar answered Oct 06 '22 20:10

Nehal J Wani