将字符串转换为对象的问题

时间:2019-07-01 12:07:04

标签: python json

我有一个json文件,我想从其中从data-estimated-earnings元素中提取a属性。该属性包含一个对象,我想从中提取open_eligible键值。

这是开始的JSON:

{"html":"<div class='car_model_estimation_result__container'>\n<div class='car_model_estimation_result cobalt-mb-tight'>\n<div class='car_model_estimation_result__item'>\n<span class=\"car_model_estimation_result_amount\">720€</span>\n<p class='cobalt-text-sectionHeader'>\n<span>maximum estimés par mois</span>\n<span class='cobalt-mb-unit cobalt-Icon cobalt-Icon--size16 cobalt-Icon--colorGraphiteLight'>\n<a class=\"js_popup_trigger\" href=\"#estimate_about_with_open\"><svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M11 9h2V7h-2v2zm1 11c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm0-18C6.477 2 2 6.477 2 12A10 10 0 1 0 12 2zm-1 15h2v-6h-2v6z\" />\n</svg>\n\n</a></span>\n</p>\n\n</div>\n<div class='owner_homepage_hero_estimation_cta__container'>\n<a class=\"owner_homepage_hero_estimation_cta--fullWidth cobalt-Button cobalt-Button--primary cobalt-Button--large js_rent_my_car js_rent_my_car_top js_estimation_result\" rel=\"nofollow\" data-tracking-params=\"{&quot;model_id&quot;:&quot;1519&quot;,&quot;brand_id&quot;:&quot;67&quot;,&quot;mileage&quot;:4,&quot;city&quot;:&quot;Anvers&quot;,&quot;release_year&quot;:2016,&quot;open_eligible&quot;:true,&quot;currency&quot;:&quot;EUR&quot;,&quot;earnings&quot;:720,&quot;earnings_period&quot;:&quot;month&quot;}\" data-click-location=\"top\" data-estimated-earnings=\"{&quot;model_id&quot;:&quot;1519&quot;,&quot;release_year&quot;:2016,&quot;mileage&quot;:4,&quot;within_eligible_area&quot;:true,&quot;open_eligible&quot;:true}\" href=\"/choose_open_or_standard?mileage=4&amp;model_id=1519&amp;open_eligible=true&amp;release_year=2016&amp;within_eligible_area=true\">Inscrire ma voiture</a>\n</div>\n</div>\n</div>\n"}

这是提取所需内容的python代码:

import json
from parsel import Selector

with open('C:/Users/coppe/Documents/py trials/estimated_earnings.json') as json_file:  
    earnings = json.load(json_file)
selector = Selector(earnings['html'])
eligibleObj = json.loads(json.dumps(selector.css('a::attr(data-estimated-earnings)').get()))
print(eligibleObj['open_eligible'])

问题是我收到此错误:

print(eligibleObj['open_eligible'])

TypeError: string indices must be integers

有人知道如何将data-estimated-earnings属性中的对象转换为对象,然后提取我需要的对象吗?

2 个答案:

答案 0 :(得分:3)

selector.css('a::attr(data-estimated-earnings)').get()返回已以字符串形式 json 序列化)的字典,因此您必须调用在其上json.dumps

>>> import json
>>> from parsel import Selector
>>>
>>> earnings = {"html":"<div class='car_model_estimation_result__container'>\n<div class='car_model_estimation_result cobalt-mb-tight'>\n<div class='car_model_estimation_result__item'>\n<span class=\"car_model_estimation_result_amount\">720€</span>\n<p class='cobalt-text-sectionHeader'>\n<span>maximum estimés par mois</span>\n<span class='cobalt-mb-unit cobalt-Icon cobalt-Icon--size16 cobalt-Icon--colorGraphiteLight'>\n<a class=\"js_popup_trigger\" href=\"#estimate_about_with_open\"><svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M11 9h2V7h-2v2zm1 11c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm0-18C6.477 2 2 6.477 2 12A10 10 0 1 0 12 2zm-1 15h2v-6h-2v6z\" />\n</svg>\n\n</a></span>\n</p>\n\n</div>\n<div class='owner_homepage_hero_estimation_cta__container'>\n<a class=\"owner_homepage_hero_estimation_cta--fullWidth cobalt-Button cobalt-Button--primary cobalt-Button--large js_rent_my_car js_rent_my_car_top js_estimation_result\" rel=\"nofollow\" data-tracking-params=\"{&quot;model_id&quot;:&quot;1519&quot;,&quot;brand_id&quot;:&quot;67&quot;,&quot;mileage&quot;:4,&quot;city&quot;:&quot;Anvers&quot;,&quot;release_year&quot;:2016,&quot;open_eligible&quot;:true,&quot;currency&quot;:&quot;EUR&quot;,&quot;earnings&quot;:720,&quot;earnings_period&quot;:&quot;month&quot;}\" data-click-location=\"top\" data-estimated-earnings=\"{&quot;model_id&quot;:&quot;1519&quot;,&quot;release_year&quot;:2016,&quot;mileage&quot;:4,&quot;within_eligible_area&quot;:true,&quot;open_eligible&quot;:true}\" href=\"/choose_open_or_standard?mileage=4&amp;model_id=1519&amp;open_eligible=true&amp;release_year=2016&amp;within_eligible_area=true\">Inscrire ma voiture</a>\n</div>\n</div>\n</div>\n"}
>>>
>>> selector = Selector(earnings['html'])
>>> selector
<Selector xpath=None data='<html><body><div class="car_model_estima'>
>>>
>>> css = selector.css('a::attr(data-estimated-earnings)').get()
>>> type(css), css
(<class 'str'>, '{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}')
>>>
>>> eligible_obj = json.loads(css)
>>> eligible_obj
{'model_id': '1519', 'release_year': 2016, 'mileage': 4, 'within_eligible_area': True, 'open_eligible': True}
>>> eligible_obj['open_eligible']
True

翻译成您的代码,应该是:

eligibleObj = json.loads(selector.css('a::attr(data-estimated-earnings)').get())

,但我会说不要在一行中进行太多操作,因为这可能会引起混乱:)。

答案 1 :(得分:0)

eligibleObjstring,看起来像这样:

'{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}'

您必须这样做:

>>> print(json.loads(eligibleObj)['open_eligible'])
True