完成html的相关练习

要求:a 打印head标签内容和你的学号后两位
b,获取body标签的内容
c. 获取id 为first的标签对象
d. 获取并打印html页面中的中文字符
# -*- coding: utf-8 -*- import re from bs4 import BeautifulSoup if __name__ == "__main__": #将本地的html文档中的数据加载到该对象中 fp = open('./test.html','r',encoding='utf-8') soup = BeautifulSoup(fp,'lxml') print(soup.head) #打印head标签内容 print(soup.find('body')) #获取body标签的内容 print(soup.find('p',id_='first')) #获取id为first的标签对象 text = soup.find('html').text print(text) regStr = ".*?([\u4E00-\u9FA5]+).*?" data = re.findall(regStr, text) print(data) # 获取并打印html页面中的中文字符
浙公网安备 33010602011771号