Python 练习册,每天一个小程序
第 0008 题: 一个HTML文件,找出里面的正文。
第 0009 题: 一个HTML文件,找出里面的链接。
0000-0010题链接
代码如下:
from bs4
import BeautifulSoup
def 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 content
sechBodyUrl(
'0007.html')
测试结果如下: