Jonathan Cheng
Jonathan Cheng

Reputation: 479

Scrapy Selector attribute

I use the following website to test:

scrapy shell http://example.webscraping.com/places/default/user/login#

And do some test:

Input 1:

response.xpath('//div//[@style]/input')

Output 1:

[<Selector xpath='//div[@style]/input' data='<input name="_next" type="hidden" value='>,  

<Selector xpath='//div[@style]/input' data='<input name="_formkey" type="hidden" val'>,  

<Selector xpath='//div[@style]/input' data='<input name="_formname" type="hidden" va'>]

Input 2:

response.xpath('//div//@style/input')

Output 2:

[]

Input 3:

response.xpath('//div//@style/input') == response.xpath('//div[style]/input')

Output 3:

True

I want to know how different 1 and 2 is,thanks.

Upvotes: 0

Views: 196

Answers (1)

Valdir Stumm Junior
Valdir Stumm Junior

Reputation: 4667

I think that you're looking for this selector:

response.xpath('//div[@style]/input')

This is how it works:

  1. select all the div elements from the document (//div);
  2. for each one of those, select only the ones which have a style attribute ([@style]);
  3. select the input nodes which are descendants of the elements selected on step 2 (/input).

Your 2nd selector (//div//@style/input) wouldn't work well because it does:

  1. select all the div elements from the page (//div);
  2. select the style attribute from every descendant of the divs selected in step 1 (//@style);
  3. select the input nodes which are direct descendants of the style attributes, which is something that doesn't exist (/input).

Upvotes: 2

Related Questions