LoginSignup
1
0

More than 1 year has passed since last update.

スクレイピング時のエラーメモ「surrogates not allowed」 - 異体字セレクタ(IVS)の削除

Last updated at Posted at 2022-09-14

背景

スクレイピングしたデータをBQにアップロードする。
そのために改行区切りのJSONを作る。
その際、余計な文字が混入していることでエラーが発生した。

エラー内容

対象の文字列
...\udb40\udd00\u539f...

エラー内容
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 40-41: surrogates not allowed

なんぞこれ?

\udb40\udd00は異体字セレクタという微妙な字形を区別するための文字コードだった。
surrogates not allowedとのことなのでこれらを無くせば良いっぽい。

削除すべき文字コードを一覧化した

https://sheilart.github.io/Azure-Alphant/surrogates.html
これ使えそうだから一覧化してみよう..2048件..なんか多いな

console.js
const elms = Array.from(document.querySelectorAll('article'));
elms.map((elm)=>{
    return elm.querySelector('p:nth-child(6)').textContent;
})

例外的な事象ぽいし、ここまでいらないや。一旦その場しのぎでこの人の判別してるやつをパクろう
IVSのセレクタ文字判別用のクラスを作ってみた

\uDB40
\uDD00
\uDDEF
\uFE00
\uFE0F
\u180B
\u180d

文字コードを削除した

このメソッドで削除後のテキストを得る

text_cleansing.py
import re

def remove_surrogates(text: str):
    return re.sub('\udb40|\udd00|\uddef|\ufe00|\ufe0f|\u180b|\u180d', '', text)

備考:代替策

その後メンテナンスが面倒そうだったので ↓ で対応した。

    def to_unicode(text:str):
        return unicodedata.normalize("NFKC", text)

参考

文字って何かね?>異体字セレクタ(IVS)
異体字セレクタってなあに
Unicodeエスケープシーケンスを変換したい
surrogates
charbase
IVSのセレクタ文字判別用のクラスを作ってみた
Unicodeエスケープシーケンス変換ツール

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0