由于电脑太卡,换了个系统,然后下载的python2.7版本。
读取本地html页面中的内容:
Demo01:
# coding=utf-8
from bs4 import BeautifulSoup
def getContent (url):
htmlfile=open(url,'r')
htmlpage=htmlfile.read()
soup=BeautifulSoup(htmlpage,"html.parser")
cctag = soup.find_all('h1', attrs={'class': 'test'})
for i in cctag:
print i.get_text()
return None
Demo02:
# coding=utf-8
import Demo01
url = r'./test.html'
Demo01.getContent(url)
test.html:
<html>
<title>
test
</title>
<body>
<h1 class="test">
这是一个小测试
</h1>
</body>
</html>
运行结果: 这是一个小测试