>
> tab = sel.css('.product-attribute.cms li')
> label_list = tab.xpath('.//*[@class="product-attribute
> cms"]/li/*[@class="label"]')
> value_list = tab.xpath('.//*[@class="product-attribute
> cms"]/li/*[@class="value"]')
The variable tab are the elements <li>, then the tab.xpath(".//...") is
applying relative selectors which in your case returns nothing.
You want either
label_list = sel.xpath('//*[@class="product-attribute
cms"]/li/*[@class="label"]')
value_list = sel.xpath('//*[@class="product-attribute
cms"]/li/*[@class="value"]')
or
label_list = tab.css('.label')
value_list = tab.css('.value')
The you will have two list of selecotrs, one with the labels and the other
with the values.
for li in zip(label_list, value_list):
> il.add_value(to_key(_x(li.css('::text'))),
> _x(li.css('::text')))
The function zip returns a list of tuples, thus the loop should be like
for label, value in zip(label_list, value_list):
il.add_value(to_key(_x(label.css('::text'))),
_x(value.css('::text')))
Alternatively, you could iterate through each li element in the tab
variable and extract ".label::text" and ".value::text", respectively. or
use ::text in the label_list and value_list extraction and have those lists
as list of strings and you will have to do something like this:
for label, value in zip(label_list, value_list):
il.add_value(to_key(label), value)
The latter approach works always that the inner elements where ::text is
applied only return a single value.
For example, if you have one value with this content:
<div class="value">Beds<br/>2</div>
Then the extraction will return this values:
In [14]: tab.css('.label ::text').extract()
Out[14]: [u'Size', u'Style', u'Categories']
In [16]: tab.css('.value ::text').extract()
Out[16]: [u'Single', u'Contemporary', u'Beds', u'2']
And the zip will not include the "2". In that case, your first approach
works better.
Regards,
Rolando
On Sun, Feb 2, 2014 at 4:04 AM, BrendanB <[email protected]> wrote:
> Hi,
>
> I'm just trying to loop through a list of li tags and extract the *label*and
> the
> *value*.
>
> I have the following code, but I cant seem to extact the data:
>
> def _load_product_details(self, il):
> sel = il.selector
> tab = sel.css('.product-attribute.cms li')
> label_list = tab.xpath('.//*[@class="product-attribute
> cms"]/li/*[@class="label"]')
> value_list = tab.xpath('.//*[@class="product-attribute
> cms"]/li/*[@class="value"]')
>
> for li in zip(label_list, value_list):
> il.add_value(to_key(_x(li.css('::text'))),
> _x(li.css('::text')))
>
>
> <!-- Example Code -->
> <ul class="product-attribute cms">
> <li class="cfx odd">
> <div class="label">Size</div>
> <div class="value">Single</div>
> </li>
> <li class="cfx even">
> <div class="label">Style</div>
> <div class="value">Contemporary</div>
> </li>
> <li class="cfx odd">
> <div class="label">Categories</div>
> <div class="value">Beds</div>
> </li>
> </ul>
>
>
> thanks
> Brendan
>
> --
> You received this message because you are subscribed to the Google Groups
> "scrapy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/scrapy-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
You received this message because you are subscribed to the Google Groups
"scrapy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/scrapy-users.
For more options, visit https://groups.google.com/groups/opt_out.