【问题标题】:How to get specific json value with regex如何使用正则表达式获取特定的 json 值
【发布时间】:2020-07-16 10:18:02
【问题描述】:

我一直试图弄清楚如何从 html 标记中获取 "form:" 值,如下所示:

window.__webpack_public_path__='https://renderer-assets.helloWorld.com/';
window.__webpack_nonce__='ca0aa826f043cfc50cc24f6ae5a2fb4a';
window.rendererAssets='["https://renderer-assets.helloWorld.com/vendors~attachment~form-container.0ca60599ecb1b55e02bf.js","https://renderer-assets.helloWorld.com/vendors~libphonenumber~submission.7a28f3a3fe6660429a73.js","https://renderer-assets.helloWorld.com/country-data.62e9eca111db492e793d.js","https://renderer-assets.helloWorld.com/form-container.c34f13ff7b2a95addd81.js","https://renderer-assets.helloWorld.com/renderer.c8aaed3fb2c5d344f80d.js","https://renderer-assets.helloWorld.com/submission.a2f19d1069d330fd0866.js","https://renderer-assets.helloWorld.com/vendors~form-container.9853dbb220cd33f30c2f.js"]';
window.rendererData= {
    scriptSrc: 'https://renderer-assets.helloworld.com/renderer.c8aaed3fb2c5d344f80d.js',
    rootDomNode: 'root',
    form: {
        "id":"Z3PvTW",
        "title":"TESTING",
        "theme": {
            "id":"xwizbR",
            "font":"Oswald",
            "name":"Plain Blue (copy)",
            "colors": {
                "question": "#3D3D3D", "answer": "#000000", "button": "#000000", "background": "#FFFFFF"
            }
            ,
            "has_transparent_button":false,
            "visibility":"private"
        }
        ,
        "workspace": {
            "href": "https:\u002F\u002Fapi.helloWorld.com\u002Fworkspaces\u002FnutqqY"
        }
        ,
        "settings": {
            "is_public":true,
            "is_trial":false,
            "language":"en",
            "progress_bar":"proportion",
            "show_progress_bar":true,
            "show_helloWorld_branding":true,
            "meta": {
                "allow_indexing": false
            }
        }
        ,
        "welcome_screens":[ {
            "ref":"a13820db-af60-40eb-823d-86cf0f20299b",
            "title":"TESTING VALUES",
            "properties": {
                "show_button": true, "button_text": "Start"
            }
        }
        ],
        "thankyou_screens":[ {
            "ref":"default_tys",
            "title":"Done! Your information was sent perfectly.",
            "properties": {
                "show_button": false, "share_icons": false
            }
        }
        ],
        "fields":[ {
            "id":"kxWycKljdtBq",
            "title":"FIRST NAME",
            "ref":"27f403f7-8c5b-4e18-b19d-1501e8f137ee",
            "validations": {
                "required": true
            }
            ,
            "type":"short_text"
        },
        {
            "id":"WEXCnZ7EAFjN",
            "title":"LAST NAME",
            "ref":"a6bf6d83-ee37-4870-b6c5-779822290cde",
            "validations": {
                "required": true
            }
            ,
            "type":"short_text"
        }

        ],
        "_links": {
            "display": "https:\u002F\u002Fautosnkr.helloWorld.com\u002Fto\u002FZ3PvTW"
        }
    }
    ,
    trackingInfo: {
        "segmentKey": "9at6spGDYXelHDdz4r0cP73b3wV1f0ri", "accountId": 12587347, "accountLimitName": "Essentials", "userId": 12586030
    }

我的意思是能够将整个表单:值放入 json.loads 中,稍后我可以按照我想要的方式对其进行修改,但在此之前我需要能够获得 form: 值我试图做的是:

regexTest = re.compile(r'form:\((.*?)\);', re.DOTALL)
data = regexTest.findall(response.text)

刚刚返回[]

我的问题是,我如何才能将整个 form: json 值转换为 getAllForm = json.loads(...)

【问题讨论】:

    标签: python json regex beautifulsoup


    【解决方案1】:
    match = re.search(r"form: ({(\s+.*){1,}\})", response.text).group(1)
    print(match)
    

    输出:

    {
            "id":"Z3PvTW",
            "title":"TESTING",
            "theme": {
                "id":"xwizbR",
                "font":"Oswald",
                "name":"Plain Blue (copy)",
                "colors": {
                    "question": "#3D3D3D", "answer": "#000000", "button": "#000000", "background": "#FFFFFF"
                }
                ,
                "has_transparent_button":false,
                "visibility":"private"
            }
            ,
            "workspace": {
                "href": "https://api.helloWorld.com/workspaces/nutqqY"
            }
            ,
            "settings": {
                "is_public":true,
                "is_trial":false,
                "language":"en",
                "progress_bar":"proportion",
                "show_progress_bar":true,
                "show_helloWorld_branding":true,
                "meta": {
                    "allow_indexing": false
                }
            }
            ,
            "welcome_screens":[ {
                "ref":"a13820db-af60-40eb-823d-86cf0f20299b",
                "title":"TESTING VALUES",
                "properties": {
                    "show_button": true, "button_text": "Start"
                }
            }
            ],
            "thankyou_screens":[ {
                "ref":"default_tys",
                "title":"Done! Your information was sent perfectly.",
                "properties": {
                    "show_button": false, "share_icons": false
                }
            }
            ],
            "fields":[ {
                "id":"kxWycKljdtBq",
                "title":"FIRST NAME",
                "ref":"27f403f7-8c5b-4e18-b19d-1501e8f137ee",
                "validations": {
                    "required": true
                }
                ,
                "type":"short_text"
            },
            {
                "id":"WEXCnZ7EAFjN",
                "title":"LAST NAME",
                "ref":"a6bf6d83-ee37-4870-b6c5-779822290cde",
                "validations": {
                    "required": true
                }
                ,
                "type":"short_text"
            }
    
            ],
            "_links": {
                "display": "https://autosnkr.helloWorld.com/to/Z3PvTW"
            }
        }
        ,
        trackingInfo: {
            "segmentKey": "9at6spGDYXelHDdz4r0cP73b3wV1f0ri", "accountId": 12587347, "accountLimitName": "Essentials", "userId": 12586030
        }
    

    现在关于将您的 JSONP 字典加载到有效的 JSON 字典中。

    检查keytrackingInfo,实际上缺少trackingInfo 以作为"trackingInfo"。所以你需要引用它,因为这将被视为重复。请查看that answer

    【讨论】:

    • 我使用了这个帮助,它实际上帮助了我:D 欣赏它!
    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多