Scrapy noob here. I am extracting an href 'rel'attribute which looks like the following:
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/div/ul/li/a/@rel').extract()
[u'"prodimage":"image_link", "intermediatezoomimage":"image_link", "fullimage":"image_link"']
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!
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.
Converting a String to Dictionary?