BeautifulSoup の .content は何をしますか? crummy.com のチュートリアルに取り組んでいますが、.content の機能がよくわかりません。フォーラムを見ましたが、回答がありません。以下のコードを見ると……。
from BeautifulSoup import BeautifulSoup
import re
doc = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
soup = BeautifulSoup(''.join(doc))
print soup.contents[0].contents[0].contents[0].contents[0].name
コードの最後の行では、代わりに「body」が出力されると思います...
File "pe_ratio.py", line 29, in <module>
print soup.contents[0].contents[0].contents[0].contents[0].name
File "C:\Python27\lib\BeautifulSoup.py", line 473, in __getattr__
raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)
AttributeError: 'NavigableString' object has no attribute 'name'
.content は、html、head、および title のみに関係していますか? もしそうなら、それはなぜですか?
事前に助けてくれてありがとう。