BeautifulSoup的.content做什么?我正在通过crummy.com's教程开展工作,我不太明白.content的作用。我看了论坛,我没有看到任何答案。看一下下面的代码....Python美丽的汤.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
我希望的代码打印出“身体”,而不是最后一行...
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'
是。内容只关注HTML ,头和标题?如果,那为什么呢?
感谢您的帮助提前。
我怀疑上述代码不起作用的原因是因为.content最初涉及html,title和head,但不是body,因为它在html层次结构中的不同类中。稍后在教程中,crummy使用下面的代码来打印身体,这让我怀疑身体是一个不同的层次结构。 head.nextSibling.name 如果有人绊倒这篇文章,重要的是阅读html结构。 结帐[http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.1][1] [1]:http://www.w3 .ORG/TR/REC-HTML40 /结构/ global.html#H-7.5.1 –