0

Scrapy noob here. I am extracting an href 'rel'attribute which looks like the following:

rel=""prodimage":"image_link","intermediatezoomimage":"image_link","fullimage":"image_link""

This can be seen as a dict like structure within the attribute.

My main goal is to obtain the image url against 'fullimage'. Hence, I want to store the response as a python dictionary. However, Xpath returns a unicode "list" ( Not just a string but a list!) with one item ( the whole rel contents as one item)

res = response.xpath('//*[@id="detail_product"]/div[1]/div[2]/ul/li[1]/a/@rel').extract()
print res

[u'"prodimage":"image_link", "intermediatezoomimage":"image_link", "fullimage":"image_link"']

type(res)

type 'list'

How do I convert the content of 'res' into something like a python dictionary ( with separated out items as list items, not just one whole item) so that I can grab individual components from the structure within 'rel'.

I hope I am clear. Thank you!

Abel
  • 56,041
  • 24
  • 146
  • 247
chetfaker
  • 11
  • 1
  • 5
  • Only values for attributes of an XML tag in the XPATH make sense for a dictionary, you can always grab that using RE. BTW you are losing the order of items because of dictionary. – g-217 Oct 04 '15 at 11:51

1 Answers1

0

SOLVED

The XPATH response above is basically a list with ONE item in unicode. Convert the respective items into strings ( using x.encode('ascii') ) and then form a string representation of a dict. In my case I had to append and prepend the string (the rel contents) with curly braces. Thats all!

Then convert that string representation of a dict into an actual dict using the method mentioned in the link below.

Convert a String representation of a Dictionary to a dictionary?

Community
  • 1
  • 1
chetfaker
  • 11
  • 1
  • 5