Beautiful Soup 4.4.0 文档介绍

Beautiful Soup是什么?


它是一个可以从HTML或XML中,提取数据的Python库。它能够实现文档导航、查找、修改。

实例演示

html_doc = """
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

"""

用BeautifulSoup解析代码,得到BeautifulSoup对象,按照标准缩进格式输出。

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')

print(soup.prttify())
# 
#  
#   
#    The Dormouse's story
#   
#  
#  
#   

# # The Dormouse's story # #

#

# Once upon a time there were three little sisters; and their names were # # Elsie # # , # # Lacie # # and # # Tillie # # ; and they lived at the bottom of a well. #

#

# ... #

# #

结构化浏览数据的方法如下:

soup.title
# The Dormouse's story

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# 

The Dormouse's story

soup.p['class'] # u'title' soup.a # Elsie soup.find_all('a') # [Elsie, # Lacie, # Tillie] soup.find(id="link3") # Tillie

从文档中找到所有的标签的链接:

for link in soup.find_all('a'):
    print(link.get('href'))
    # http://example.com/elsie
    # http://example.com/lacie
    # http://example.com/tillie

从文档中获取所有文字内容:

print(soup.get_text())
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...

你可能感兴趣的:(Beautiful Soup 4.4.0 文档介绍)