こんにちは、ミナピピン(@python_mllover)です!
beautifulsoupのreplace_with()でタグを変換する際に<などが入っていると関数を実行した際にエスケープされてlt;みたいな特殊文字に変換されてしまう際の解決法をメモっておきたいと思います。
import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
from xml.sax.saxutils import unescape
headers = {
'User-Agent':'Mozilla/5.0'
}
#対象のサイトURL
url = 'https://swallow.5ch.net/test/read.cgi/livejupiter/1627502377/'
#URLリソースを開く
res = requests.get(url, headers=headers)
unker_data = []
main_data = []
unker_number = []
#インスタンスの作成
soup = BeautifulSoup(res.content, "html.parser")
post = soup.find_all('div', class_='post')[3]
text = post.find_all('span', class_='escaped')[0]
try:
for tag in text.find_all('a', class_='image'):
tag.replace_with('<img src="' + tag.text + '" width="400" height="400"></img>')
except:
pass
print(post)
解決法
解決法はxml.sax.saxutilsのunescape()で置換することで元の記号に戻せます。
from xml.sax.saxutils import unescape text = '「&」「<」「>」' print(unescape(text)) # => '& < >'
参照:https://www.takasay.com/entry/2015/07/07/095739
コメント
# => ‘& ‘
ただしくは
# => ‘「&」 「」’
ではありませんか?
あっそうですね、ご指摘ありがとうございます!