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

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

from bs4 import BeautifulSoup
def sechBodyURL(path):
    #此處因為我的html檔案編碼格式為gbk,因此加了encoding
    fp=open(path,encoding='gbk',errors='ignore')
    text=BeautifulSoup(fp,'html.parser')
    urls=text.findAll('a')
    for u in urls:
        print(u['href'])
    content=text.get_text().strip()
    print(content)
    return content
sechBodyURL('20test.html')

執行結果如下,中文存在亂碼,是因為原html檔案編碼問題