titleTag = soup.html.head.title
titleTag
#<title>Page title</title>
titleTag.string
# u'Page title'
len(soup('p'))
# 2
soup.findAll('p', align="center")
# [<p id="firstpara" align="center">This is paragraph <b>one</b>.</p>]
soup.find('p', align="center")
#<p id="firstpara" align="center">This is paragraph <b>one</b>.</p>
soup('p', align="center")[0]['id']
# u'firstpara'
soup.find('p', align=re.compile('^b.*'))['id']
# u'secondpara'
soup.find('p').b.string
# u'one'
soup('p')[1].b.string
# u'two'
当然也可以简单地修改文档
titleTag['id'] = 'theTitle'
titleTag.contents[0].replaceWith("New title")
soup.html.head
# <head><title id="theTitle">New title</title></head>
soup.p.extract()
soup.prettify()
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <p id="secondpara" align="blah">
# This is paragraph
# <b>
# two
# </b>
# .
# </p>
# </body>
# </html>
soup.p.replaceWith(soup.b)
# <html>
# <head>
# <title id="theTitle">
# New title
# </title>
# </head>
# <body>
# <b>
# two
# </b>
# </body>
# </html>
soup.body.insert(0, "This page used to have ")
soup.body.insert(2, " <p> tags!")
soup.body
# <body>This page used to have <b>two</b> <p> tags!</body>
最后,为大家提供 Beautiful Soup 的文档。希望能对您有帮助。
英文原文:http://www.crummy.com/software/BeautifulSoup/ (翻译有删节,请查看原文链接)
0
三角符文第一章下载
角色扮演 下载
嘀嘀动画官方正版下载
趣味娱乐 下载
像素世界僵尸危机安卓版
飞行射击 下载