我有一个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=\"{"model_id":"1519","brand_id":"67","mileage":4,"city":"Anvers","release_year":2016,"open_eligible":true,"currency":"EUR","earnings":720,"earnings_period":"month"}\" data-click-location=\"top\" data-estimated-earnings=\"{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}\" href=\"/choose_open_or_standard?mileage=4&model_id=1519&open_eligible=true&release_year=2016&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
属性中的对象转换为对象,然后提取我需要的对象吗?
答案 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=\"{"model_id":"1519","brand_id":"67","mileage":4,"city":"Anvers","release_year":2016,"open_eligible":true,"currency":"EUR","earnings":720,"earnings_period":"month"}\" data-click-location=\"top\" data-estimated-earnings=\"{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}\" href=\"/choose_open_or_standard?mileage=4&model_id=1519&open_eligible=true&release_year=2016&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)
您eligibleObj
是string
,看起来像这样:
'{"model_id":"1519","release_year":2016,"mileage":4,"within_eligible_area":true,"open_eligible":true}'
您必须这样做:
>>> print(json.loads(eligibleObj)['open_eligible'])
True