安装: (Ubuntu) sudo apt-get install python-bs4 或者 pip install beautifulsoup4 或着 easy_install beautifulsoup4 一些基本应用: from bs4 import BeautifulSoup import re ,urllib2 url="http://data.eastmoney.com/cjsj/cpi.html" data=urllib2.urlopen(url) soup=BeautifulSoup(data,from_encoding="utf8") ++++++标签的tring,strings,get_text(),contents,get方法。navigablestring对象有属性string,strings+++++++++++ 获取head soup.head() for i in soup.head.strings: ... print i 居民消费价格指数(CPI) _ 数据中心 _ 东方财富网 var swf_line = "http://g1.dfcfw.com/g1/201012/20101214085507.swf"; var swf_pie = "http://g1.dfcfw.com/g1/201104/20110412125826.swf"; var swf_column = " 获取title print soup.title.get_text() print soup.title.string print soup.title.contents[0] 居民消费价格指数(CPI) _ 数据中心 _ 东方财富网 获取标签中含有(class='secondTr')的对象 soup.find(class_='secondTr') (python对class是敏感词 ,因此写成class_) 获取tr标签且tr标签的class属性以Tr结尾 print soup.find_all('tr',class_=re.compile('Tr$')) 获取全部超连接 print soup.find_all('a') 获取全部href以 for i in soup.find_all('a',href=re.compile('^: print i['href'] #i.get('href') navigablestring能够很简单地转换为unicode,和string是几乎同样的。 实例: unicode_string = unicode(tag.string)