2017-01-08 11 views
0

xmlタグ内のテキストをBeautifulSoupオブジェクトに変更した後で、単純に変更したいと考えています。BeautifulSoup4:xmlタグ内のテキストを変更する

現在のコード:私のコンソールでこのコードを実行している

example_string = '<conversion><person>John</person></conversion>' 
bsoup = BeautifulSoup(example_string) 
bsoup.person.text = 'Michael' 

は、このエラーをレンダリングする:

Traceback (most recent call last): 
    File "<stdin>", line 3, in <module> 
AttributeError: can't set attribute 

にはどうすればperson xmlタグ内の値を変更できますか?

答えて

0

あなたが読み取り専用されている.text.string attributeをないように設定する必要があります。

example_string = '<conversion><person>John</person></conversion>' 
bsoup = BeautifulSoup(example_string, "xml") 
bsoup.person.string = 'Michael' 

デモ:助けを

In [1]: from bs4 import BeautifulSoup 
    ...: 
    ...: 
    ...: example_string = '<conversion><person>John</person></conversion>' 
    ...: bsoup = BeautifulSoup(example_string, "xml") 
    ...: bsoup.person.string = 'Michael' 
    ...: 
    ...: print(bsoup.prettify()) 
    ...: 
<?xml version="1.0" encoding="utf-8"?> 
<conversion> 
<person> 
    Michael 
</person> 
</conversion> 
+0

おかげで、完璧に動作します – Paul

関連する問題