python 一个HTML文件,找出正文和链接

来源:互联网 发布:github 自定义域名 编辑:程序博客网 时间:2024/06/06 21:06

Python 练习册,每天一个小程序

第 0008 题: 一个HTML文件,找出里面的正文。

第 0009 题: 一个HTML文件,找出里面的链接。

0000-0010题链接

代码如下:

# coding=utf-8from bs4 import BeautifulSoupdef sechBodyUrl(path):    with open(path,encoding='utf-8') as fp:        text = BeautifulSoup(fp, 'lxml')        urls = text.findAll('a')        for u in urls:            print(u['href'])        content = text.get_text().strip('\n')    return contentsechBodyUrl('0007.html')#print(searchBody('0007.html'))

测试结果如下:
这里写图片描述