<a>タグ内の文章の中にある<br>タグを\nに変換してデータベースに保存するサンプルコードがないか探し、スタックオーバーフローの質問にコードと一緒に投稿されていた回答を見つけましたが、いまいちif else行の処理が分かりません・・・どなたか詳しく教えてください!
下記がそのコードです。
Python
1import lxml.html as html 2 3fragment = '<div>This is a text node.<br/>This is another text node.<br/><br/><span>And a child element.</span><span>Another child,<br> with two text nodes</span></div>' 4doc = html.document_fromstring(fragment) 5for br in doc.xpath("*//br"): 6 br.tail = "\n" + br.tail if br.tail else "\n"
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/30 14:48
2018/10/30 14:51
2018/10/30 14:55