Beautiful Soup 是用Python寫的一個HTML/XML的解析器,它能夠很好的處理不規範標記並生成剖析樹(parse tree)。 它提供簡單又經常使用的導航(navigating),搜索以及修改剖析樹的操做。它能夠大大節省你的編程時間。html
# -*- coding:utf-8 -*- #導入所須要的模塊 from bs4 import BeautifulSoup import re #一段html的字符串 html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ #建立beautifulsoup對象 soup = BeautifulSoup(html_doc, 'html.parser', from_encoding = 'utf-8') #查找html文檔中出現的全部連接 print '獲取全部的連接:' links = soup.find_all('a') for link in links: print link.name, link['href'], link.get_text() #查找html文檔中lacie的連接 print '獲取lacie的連接:' link_node = soup.find('a', href = 'http://example.com/lacie') print link_node.name, link_node['href'], link_node.get_text() #利用正則表達式進行模糊匹配 print '正則模糊匹配:' link_node = soup.find('a', href = re.compile(r"ill")) print link_node.name, link_node['href'], link_node.get_text() #獲取標題的文字 print '獲取p段落文字:' link_node = soup.find('p', class_ = "title") print link_node.name, link_node.get_text()
本文爲學習imooc筆記node