HTML解析器BeautifulSoup

BeautifulSoup是Python的一个库,可解析用urllib2抓取下来的HTML

1.Beautiful Soup 安装

可以利用 pip 来安装,在Python程序中导入

pip install beautifulsoup4

2.在Python中导入

from BeautifulSoup import BeautifulSoup

3.创建 beautifulsoup 对象

soup = BeautifulSoup(html)

4.beautifulsoup 的使用方法

拿到第一个标签的内容:.title()

想要获取的内容为utf-8格式需要使用.decode方法

print str(soup.title).decode('utf-8')

获取某标签的某属性值:find_all( name , attrs , recursive , text , **kwargs )

p_detail = soup.find("p")  

 

原文地址:https://www.cnblogs.com/corolcorona/p/6668695.html