如果你想提取網(wǎng)頁上的文章內(nèi)容,readability這個免費好用的工具絕對值得一試
官網(wǎng):https://www.readability.com/
提取內(nèi)容的api文檔:https://www.readability.com/developers/api/parser
注冊一下炸枣,在個人頁面可以找到你自己的token
API - GET請求,帶上token和url參數(shù)
https://www.readability.com/api/content/v1/parser?token=your_token&url=url_you_want_to_parse
響應(yīng)示例---json格式返回數(shù)據(jù)
響應(yīng)
來看個中文的
隨便一篇網(wǎng)易博客
content部分就是提取的網(wǎng)頁內(nèi)容了捕捂,將其寫入html文件蚀之,可以直接打開顯示網(wǎng)頁內(nèi)容
如果你只是為了提取和保存內(nèi)容邑滨,到這里就可以了。
如果你需要得到網(wǎng)頁內(nèi)容捅僵,并進(jìn)行一些處理家卖,那可能就得把&#x開頭的內(nèi)容轉(zhuǎn)換成中文了&#x開頭的是什么編碼?庙楚,可能需要進(jìn)行以下操作
# 去掉content中的html標(biāo)記
def remove_html_tag(content):
return re.sub(r'</?\w+[^>]*>', '', content)
# 轉(zhuǎn)換成中文
def convert_to_cn(text):
# 需要將 × 這種先做補全上荡,×
text = re.sub(r'&#x([A-F0-9]{2});', r'�\1;', text)
return text.replace('&#x', '\u') \
.replace(';', '') \
.decode('unicode-escape') \
.encode('utf-8')