從Google網頁中透過正規表示式獲取json如何轉換unicode物件

xqs42b發表於2024-04-13

場景:Google爬蟲,獲取下拉框搜尋關鍵詞,需要獲取頁面的裡面的json字串如下面:

'{\\x22aa\\x22:{},\\x22abd\\x22:{\\x22abd\\x22:false,\\x22deb\\x22:false,\\x22det\\x22:false},\\x22async\\x22:{},\\x22attn\\x22:{},\\x22bgd\\x22:{\\x22ac\\x22:true,\\x22as\\x22:true,\\x22at\\x22:0,\\x22ea\\x22:true,\\x22ed\\x22:0,\\x22ei\\x22:true,\\x22el\\x22:true,\\x22ep\\x22:true,\\x22er\\x22:true,\\x22et\\x22:0,\\x22eu\\x22:false,\\x22li\\x22:false,\\x22wl\\x22:false},\\x22cdos\\x22:{\\x22cdobsel\\x22:false},\\x22csi\\x22:{},\\x22d\\x22:{},\\x22foot\\x22:{\\x22dv\\x22:\\x22A13OmSDjAawaEKMYZTqdNnEqnlN77Rg\\x22},\\x22gf\\x22:{\\x22pid\\x22:196},\\x22gwc\\x22:{},\\x22hsm\\x22:{},\\x22jsa\\x22:{\\x22csi\\x22:true,\\x22csir\\x22:100},\\x22kyn\\x22:{},\\x22lli\\x22:{},\\x22mb4ZUb\\x22:{},\\x22pHXghd\\x22:{},\\x22sb_wiz\\x22:{\\x22onf\\x22:\\x22EAEYj6vGoaypudTUARjAp8368N6Oq2sY1P78svbOpoFWGIeIjfvlpdGDVhjtkpzIgLGFiJsBMvcBChcKFeibi-eZvei0qOe7hOWtpui0qOiwsQodChvom4vnmb3otKjnu4TlrabmlbDmja7liIbmnpAKGgoY6JuL55m96LSo6LCx57uT5p6c5YiG5p6QCh0KG-ibi-eZvei0qOe7hOWtpuWIhuaekOa1geeoiwoUChLom4vnmb3otKjnu4TlraZwcHQKFwoV6JuL55m96LSo57uE5a2m5YWs5Y-4ChEKD-ibi-eZvee7hOa1i-W6jwoaChjom4vnmb3otKjnu4TlrabmlbDmja7lupMKEgoQ6JuL55m96LSoIOeglOeptgoOCgzotKjosLHmo4DmtYsQRzJpCh0KG-ibi-eZvei0qOacieWTquS6m-enjeexu--8nwodChvom4vnmb3otKjmnInku4DkuYjlhYPntKDvvJ8KJgok6JuL55m96LSo55qE57uE5oiQ6YOo5YiG5piv5LuA5LmI77yfEOQC\\x22,\\x22scq\\x22:\\x22\\x22,\\x22stok\\x22:\\x22DB6NHG6owl0pg5mSRV3RZ-SroY0\\x22,\\x22zps\\x22:\\x22GI-rxqGsqbnU1AEYwKfN-vDejqtrGNT-_LL2zqaBVhiHiI375aXRg1YY7ZKcyICxhYibAQ\\x22},\\x22sf\\x22:{},\\x22sonic\\x22:{},\\x22spch\\x22:{\\x22ae\\x22:\\x22\\\\u8bf7\\\\u68c0\\\\u67e5\\\\u60a8\\\\u7684\\\\u9ea6\\\\u514b\\\\u98ce\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u4e86\\\\u89e3\\\\u8be6\\\\u60c5\\\\u003C/a\\\\u003E\\x22,\\x22ak\\x22:\\x22\\x22,\\x22ao\\x22:true,\\x22cd\\x22:0,\\x22fp\\x22:false,\\x22hl\\x22:\\x22zh-Hans-SG\\x22,\\x22im\\x22:\\x22\\\\u70b9\\\\u51fb\\\\u003Cb\\\\u003E\\\\u5141\\\\u8bb8\\\\u003C/b\\\\u003E\\\\u5f00\\\\u59cb\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\x22,\\x22iw\\x22:\\x22\\\\u6b63\\\\u5728\\\\u7b49\\\\u5f85...\\x22,\\x22lm\\x22:\\x22\\\\u6b63\\\\u5728\\\\u6536\\\\u542c\\\\u2026\\x22,\\x22lu\\x22:\\x22\\\\u76ee\\\\u524d\\\\u5c1a\\\\u672a\\\\u63d0\\\\u4f9b%1$s\\\\u7248\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\\\u670d\\\\u52a1\\x22,\\x22mb\\x22:false,\\x22mc\\x22:false,\\x22ne\\x22:\\x22\\\\u672a\\\\u8fde\\\\u63a5\\\\u5230\\\\u4e92\\\\u8054\\\\u7f51\\x22,\\x22nt\\x22:\\x22\\\\u4e0d\\\\u786e\\\\u5b9a\\\\u6240\\\\u8bf4\\\\u5185\\\\u5bb9\\\\uff0c\\\\u003Cspan\\\\u003E\\\\u8bf7\\\\u91cd\\\\u8bd5\\\\u003C/span\\\\u003E\\x22,\\x22nv\\x22:\\x22\\\\u8bf7\\\\u68c0\\\\u67e5\\\\u60a8\\\\u7684\\\\u9ea6\\\\u514b\\\\u98ce\\\\u548c\\\\u97f3\\\\u9891\\\\u7535\\\\u5e73\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u4e86\\\\u89e3\\\\u8be6\\\\u60c5\\\\u003C/a\\\\u003E\\x22,\\x22pe\\x22:\\x22\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\\\u529f\\\\u80fd\\\\u5904\\\\u4e8e\\\\u5173\\\\u95ed\\\\u72b6\\\\u6001\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u8be6\\\\u7ec6\\\\u4fe1\\\\u606f\\\\u003C/a\\\\u003E\\x22,\\x22ri\\x22:false,\\x22rm\\x22:\\x22\\\\u8bf7\\\\u5f00\\\\u59cb\\\\u8bf4\\\\u8bdd\\x22,\\x22s3\\x22:false,\\x22sa\\x22:false},\\x22tl\\x22:{\\x22rvkey\\x22:\\x22AIzaSyC_9Rt88UMjzgg5pIVArnfuIVkJx4zCdTY\\x22}}'

然後我透過正在表示式獲取後,透過json.loads()報一下錯誤

報錯:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

轉換程式碼如下:

pmc_json = '''
{\\x22aa\\x22:{},\\x22abd\\x22:{\\x22abd\\x22:false,\\x22deb\\x22:false,\\x22det\\x22:false},\\x22async\\x22:{},\\x22attn\\x22:{},\\x22bgd\\x22:{\\x22ac\\x22:true,\\x22as\\x22:true,\\x22at\\x22:0,\\x22ea\\x22:true,\\x22ed\\x22:0,\\x22ei\\x22:true,\\x22el\\x22:true,\\x22ep\\x22:true,\\x22er\\x22:true,\\x22et\\x22:0,\\x22eu\\x22:false,\\x22li\\x22:false,\\x22wl\\x22:false},\\x22cdos\\x22:{\\x22cdobsel\\x22:false},\\x22csi\\x22:{},\\x22d\\x22:{},\\x22foot\\x22:{\\x22dv\\x22:\\x22A13OmSDjAawaEKMYZTqdNnEqnlN77Rg\\x22},\\x22gf\\x22:{\\x22pid\\x22:196},\\x22gwc\\x22:{},\\x22hsm\\x22:{},\\x22jsa\\x22:{\\x22csi\\x22:true,\\x22csir\\x22:100},\\x22kyn\\x22:{},\\x22lli\\x22:{},\\x22mb4ZUb\\x22:{},\\x22pHXghd\\x22:{},\\x22sb_wiz\\x22:{\\x22onf\\x22:\\x22EAEYj6vGoaypudTUARjAp8368N6Oq2sY1P78svbOpoFWGIeIjfvlpdGDVhjtkpzIgLGFiJsBMvcBChcKFeibi-eZvei0qOe7hOWtpui0qOiwsQodChvom4vnmb3otKjnu4TlrabmlbDmja7liIbmnpAKGgoY6JuL55m96LSo6LCx57uT5p6c5YiG5p6QCh0KG-ibi-eZvei0qOe7hOWtpuWIhuaekOa1geeoiwoUChLom4vnmb3otKjnu4TlraZwcHQKFwoV6JuL55m96LSo57uE5a2m5YWs5Y-4ChEKD-ibi-eZvee7hOa1i-W6jwoaChjom4vnmb3otKjnu4TlrabmlbDmja7lupMKEgoQ6JuL55m96LSoIOeglOeptgoOCgzotKjosLHmo4DmtYsQRzJpCh0KG-ibi-eZvei0qOacieWTquS6m-enjeexu--8nwodChvom4vnmb3otKjmnInku4DkuYjlhYPntKDvvJ8KJgok6JuL55m96LSo55qE57uE5oiQ6YOo5YiG5piv5LuA5LmI77yfEOQC\\x22,\\x22scq\\x22:\\x22\\x22,\\x22stok\\x22:\\x22DB6NHG6owl0pg5mSRV3RZ-SroY0\\x22,\\x22zps\\x22:\\x22GI-rxqGsqbnU1AEYwKfN-vDejqtrGNT-_LL2zqaBVhiHiI375aXRg1YY7ZKcyICxhYibAQ\\x22},\\x22sf\\x22:{},\\x22sonic\\x22:{},\\x22spch\\x22:{\\x22ae\\x22:\\x22\\\\u8bf7\\\\u68c0\\\\u67e5\\\\u60a8\\\\u7684\\\\u9ea6\\\\u514b\\\\u98ce\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u4e86\\\\u89e3\\\\u8be6\\\\u60c5\\\\u003C/a\\\\u003E\\x22,\\x22ak\\x22:\\x22\\x22,\\x22ao\\x22:true,\\x22cd\\x22:0,\\x22fp\\x22:false,\\x22hl\\x22:\\x22zh-Hans-SG\\x22,\\x22im\\x22:\\x22\\\\u70b9\\\\u51fb\\\\u003Cb\\\\u003E\\\\u5141\\\\u8bb8\\\\u003C/b\\\\u003E\\\\u5f00\\\\u59cb\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\x22,\\x22iw\\x22:\\x22\\\\u6b63\\\\u5728\\\\u7b49\\\\u5f85...\\x22,\\x22lm\\x22:\\x22\\\\u6b63\\\\u5728\\\\u6536\\\\u542c\\\\u2026\\x22,\\x22lu\\x22:\\x22\\\\u76ee\\\\u524d\\\\u5c1a\\\\u672a\\\\u63d0\\\\u4f9b%1$s\\\\u7248\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\\\u670d\\\\u52a1\\x22,\\x22mb\\x22:false,\\x22mc\\x22:false,\\x22ne\\x22:\\x22\\\\u672a\\\\u8fde\\\\u63a5\\\\u5230\\\\u4e92\\\\u8054\\\\u7f51\\x22,\\x22nt\\x22:\\x22\\\\u4e0d\\\\u786e\\\\u5b9a\\\\u6240\\\\u8bf4\\\\u5185\\\\u5bb9\\\\uff0c\\\\u003Cspan\\\\u003E\\\\u8bf7\\\\u91cd\\\\u8bd5\\\\u003C/span\\\\u003E\\x22,\\x22nv\\x22:\\x22\\\\u8bf7\\\\u68c0\\\\u67e5\\\\u60a8\\\\u7684\\\\u9ea6\\\\u514b\\\\u98ce\\\\u548c\\\\u97f3\\\\u9891\\\\u7535\\\\u5e73\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u4e86\\\\u89e3\\\\u8be6\\\\u60c5\\\\u003C/a\\\\u003E\\x22,\\x22pe\\x22:\\x22\\\\u8bed\\\\u97f3\\\\u641c\\\\u7d22\\\\u529f\\\\u80fd\\\\u5904\\\\u4e8e\\\\u5173\\\\u95ed\\\\u72b6\\\\u6001\\\\u3002\\\\u003Ca href\\x3d\\\\\\x22https://support.google.com/chrome/?p\\x3dui_voice_search\\\\\\x22 target\\x3d\\\\\\x22_blank\\\\\\x22\\\\u003E\\\\u8be6\\\\u7ec6\\\\u4fe1\\\\u606f\\\\u003C/a\\\\u003E\\x22,\\x22ri\\x22:false,\\x22rm\\x22:\\x22\\\\u8bf7\\\\u5f00\\\\u59cb\\\\u8bf4\\\\u8bdd\\x22,\\x22s3\\x22:false,\\x22sa\\x22:false},\\x22tl\\x22:{\\x22rvkey\\x22:\\x22AIzaSyC_9Rt88UMjzgg5pIVArnfuIVkJx4zCdTY\\x22}}'
'''
pmc_json = bytes(pmc_json, "utf-8").decode("unicode_escape")
pmc = json.loads(pmc_json)

結果如下:

{'aa': {}, 'abd': {'abd': False, 'deb': False, 'det': False}, 'async': {}, 'attn': {}, 'bgd': {'ac': True, 'as': True, 'at': 0, 'ea': True, 'ed': 0, 'ei': True, 'el': True, 'ep': True, 'er': True, 'et': 0, 'eu': False, 'li': False, 'wl': False}, 'cdos': {'cdobsel': False}, 'csi': {}, 'd': {}, 'foot': {'dv': 'A13OmSDjAawaEKMYZTqdNnEqnlN77Rg'}, 'gf': {'pid': 196}, 'gwc': {}, 'hsm': {}, 'jsa': {'csi': True, 'csir': 100}, 'kyn': {}, 'lli': {}, 'mb4ZUb': {}, 'pHXghd': {}, 'sb_wiz': {'onf': 'EAEYj6vGoaypudTUARjAp8368N6Oq2sY1P78svbOpoFWGIeIjfvlpdGDVhjtkpzIgLGFiJsBMvcBChcKFeibi-eZvei0qOe7hOWtpui0qOiwsQodChvom4vnmb3otKjnu4TlrabmlbDmja7liIbmnpAKGgoY6JuL55m96LSo6LCx57uT5p6c5YiG5p6QCh0KG-ibi-eZvei0qOe7hOWtpuWIhuaekOa1geeoiwoUChLom4vnmb3otKjnu4TlraZwcHQKFwoV6JuL55m96LSo57uE5a2m5YWs5Y-4ChEKD-ibi-eZvee7hOa1i-W6jwoaChjom4vnmb3otKjnu4TlrabmlbDmja7lupMKEgoQ6JuL55m96LSoIOeglOeptgoOCgzotKjosLHmo4DmtYsQRzJpCh0KG-ibi-eZvei0qOacieWTquS6m-enjeexu--8nwodChvom4vnmb3otKjmnInku4DkuYjlhYPntKDvvJ8KJgok6JuL55m96LSo55qE57uE5oiQ6YOo5YiG5piv5LuA5LmI77yfEOQC', 'scq': '', 'stok': 'DB6NHG6owl0pg5mSRV3RZ-SroY0', 'zps': 'GI-rxqGsqbnU1AEYwKfN-vDejqtrGNT-_LL2zqaBVhiHiI375aXRg1YY7ZKcyICxhYibAQ'}, 'sf': {}, 'sonic': {}, 'spch': {'ae': '請檢查您的麥克風。<a href="https://support.google.com/chrome/?p=ui_voice_search" target="_blank">瞭解詳情</a>', 'ak': '', 'ao': True, 'cd': 0, 'fp': False, 'hl': 'zh-Hans-SG', 'im': '點選<b>允許</b>開始語音搜尋', 'iw': '正在等待...', 'lm': '正在收聽…', 'lu': '目前尚未提供%1$s版語音搜尋服務', 'mb': False, 'mc': False, 'ne': '未連線到網際網路', 'nt': '不確定所說內容,<span>請重試</span>', 'nv': '請檢查您的麥克風和音訊電平。<a href="https://support.google.com/chrome/?p=ui_voice_search" target="_blank">瞭解詳情</a>', 'pe': '語音搜尋功能處於關閉狀態。<a href="https://support.google.com/chrome/?p=ui_voice_search" target="_blank">詳細資訊</a>', 'ri': False, 'rm': '請開始說話', 's3': False, 'sa': False}, 'tl': {'rvkey': 'AIzaSyC_9Rt88UMjzgg5pIVArnfuIVkJx4zCdTY'}}

感想:

解決這個問題,差不多花了我半天的時間。剛開始透過正規表示式獲取的json字元,然後透過print直接列印在python終端,看起來還是比較正常的,看不出來有啥問題;

然後我透過print(repr())列印就能明顯的看出區別,然後把它編碼成utf-8,然後在轉化為unicode完美解決這個問題。

相關文章