问题描述:

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!

网友答案:

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.

Converting a String to Dictionary?

相关阅读:
Top