回答編集履歴

2

修正

2019/08/11 22:53

投稿

can110
can110

スコア38266

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  --
20
20
 
21
- 提示されたデータとコード片でも`Win10,Python3.6.7,コマンドプロンプト`環境にて`print`できました。
21
+ 提示されたデータとコード片でも`Win10,Python3.6.7,コマンドプロンプト`環境にて`print`できました。また、ファイル出力結果も正常です。
22
22
 
23
23
  ```Python
24
24
 
@@ -94,6 +94,18 @@
94
94
 
95
95
  comment = comment.encode('utf-16', 'surrogatepass').decode('utf-16')
96
96
 
97
+
98
+
99
+ # ファイルに出力
100
+
101
+ with open('ret.txt', 'w', encoding='utf-16') as f:
102
+
103
+ f.write(comment)
104
+
105
+
106
+
97
107
  print(comment) # 長崎出身です????
98
108
 
99
109
  ```
110
+
111
+ ![イメージ説明](a01b314bbe728286e0173bec3c117357.png)

1

追記

2019/08/11 22:53

投稿

can110
can110

スコア38266

test CHANGED
@@ -11,3 +11,89 @@
11
11
  print(s) # 長崎出身です????
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ 追記
18
+
19
+ --
20
+
21
+ 提示されたデータとコード片でも`Win10,Python3.6.7,コマンドプロンプト`環境にて`print`できました。
22
+
23
+ ```Python
24
+
25
+ import re
26
+
27
+
28
+
29
+ page_source = """window.content_Data_k = {
30
+
31
+ "config": {
32
+
33
+ "token": "dsf78sadf9sdfasd7s9df7safd6",
34
+
35
+ "seer": null,
36
+
37
+ "seerId": null
38
+
39
+ },
40
+
41
+ "country_code": "JP",
42
+
43
+ "language_code": "ja",
44
+
45
+ "locale": "ja_JP",
46
+
47
+ "index_data": {
48
+
49
+ "contentPage": [{
50
+
51
+ "logging_page_id": "contentPage_728394298",
52
+
53
+ "show_suggested_contents": false,
54
+
55
+ "graphql": {
56
+
57
+ "user": {
58
+
59
+ "name": "aaa",
60
+
61
+ "comment":"\u9577\u5d0e\u51fa\u8eab\u3067\u3059\ud83d\udd4a",
62
+
63
+ "block": false,
64
+
65
+ "country_block": false,
66
+
67
+ "expect_url": null,
68
+
69
+ "expect_url_linkshimmed": null,
70
+
71
+ "key_number": {
72
+
73
+ "count": 13
74
+
75
+ }
76
+
77
+ }
78
+
79
+ }
80
+
81
+ }]
82
+
83
+ }
84
+
85
+ }"""
86
+
87
+
88
+
89
+ comment = re.search(r'"comment"\s*:\s*"([^"]*)"',page_source).group(1)
90
+
91
+ #print(comment) # UnicodeEncodeError: 'utf-8' codec can't encode characters in position 6-7: surrogates not allowed
92
+
93
+
94
+
95
+ comment = comment.encode('utf-16', 'surrogatepass').decode('utf-16')
96
+
97
+ print(comment) # 長崎出身です????
98
+
99
+ ```