1. 程式人生 > 其它 >python 一個HTML檔案,找出正文和連結

python 一個HTML檔案,找出正文和連結

技術標籤:Python小技巧

ython 練習冊,每天一個小程式

第 0008 題: 一個HTML檔案,找出裡面的正文。

第 0009 題: 一個HTML檔案,找出裡面的連結。

0000-0010題連結

程式碼如下:

# coding=utf-8
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')
#print(searchBody('0007.html'))

測試結果如下:
這裡寫圖片描述