JSON的格式不正确(碎片)

2024-10-03 02:38:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,我正在尝试从网站上获取一些数据。 但当我执行下面的代码时,我很难做到这一点。我从页面的撇号中获取的值不是json的有效格式

差不多

[{'companyId': 1,
                                              'companyPhotoId': 9120,
                                              'description': 'Pracovní '
                                                             'prostory',
                                              'fileId': '4ec99adf-f89b-481d-8f6d-3d2f49b1f1f1',
                                              'isThumbHorizontal': False,
                                              'order': 1,
                                              'thumbnailFileId': 'e00c3c9c-55d3-4bad-bd5a-d485bfab2986'},
                                             {'companyId': 1,
                                              'companyPhotoId': 9121,
                                              'description': 'mDevcamp 2018',
                                              'fileId': '089dfef5-5c89-4e56-ad49-c6458d258a3f',
                                              'isThumbHorizontal': False,
                                              'order': 2,
                                              'thumbnailFileId': '411cbd66-dbb4-4385-8ae9-cc89f8787346'},
                                             {'companyId': 1,
                                              'companyPhotoId': 9122,
                                              'description': 'Kancl 2018',
                                              'fileId': 'fcdadaeb-3960-45be-b575-0a0be34a73bc',
                                              'isThumbHorizontal': True,
                                              'order': 3,
                                              'thumbnailFileId': '7cd162e9-1d18-4629-b685-9b4246637fef'}]
import scrapy
from pprint import pprint
import json

class Project1SpiderSpider(scrapy.Spider):
    name = 'project1-spider'
    allowed_domains = ['somewebsite']
    start_urls = ['somewebsite'.format(i + 1) for i in range(2000)]

    def parse(self, response):
        results = json.loads(response.body)
        pprint(results)

我需要这样的格式

[{"companyId": 1,
                                              "companyPhotoId": 9120,
                                              "description": "Pracovní "
                                                             "prostory",
                                              "fileId": "4ec99adf-f89b-481d-8f6d-3d2f49b1f1f1",
                                              "isThumbHorizontal": False,
                                              "order": 1,
                                              "thumbnailFileId": "e00c3c9c-55d3-4bad-bd5a-d485bfab2986"},
                                             {"companyId": 1,
                                              "companyPhotoId": 9121,
                                              "description": "mDevcamp 2018",
                                              "fileId": "089dfef5-5c89-4e56-ad49-c6458d258a3f",
                                              "isThumbHorizontal": False,
                                              "order": 2,
                                              "thumbnailFileId": "411cbd66-dbb4-4385-8ae9-cc89f8787346"},
                                             {"companyId": 1,
                                              "companyPhotoId": 9122,
                                              "description": "Kancl 2018",
                                              "fileId": "fcdadaeb-3960-45be-b575-0a0be34a73bc",
                                              "isThumbHorizontal": True,
                                              "order": 3,
                                              "thumbnailFileId": "7cd162e9-1d18-4629-b685-9b4246637fef"}]

你能帮我看看代码应该是什么样子吗

多谢各位


Tags: 代码importjsonfalse格式orderdescriptionpprint
1条回答
网友
1楼 · 发布于 2024-10-03 02:38:45

当您执行json.loads(response.body)操作时,它将从json字符串转换为python对象。得到结果是因为打印了python对象

为了得到想要的结果,您应该打印原始的json:print(response.body),或者如果您想很好地打印它,您应该将python对象转换为带有缩进的json字符串,即print(json.dumps(results, indent=2))

    def parse(self, response):
        # Get a python object
        results = json.loads(response.body)
        # Pretty print the json
        print(json.dumps(results, indent=2))

相关问题 更多 >