【参考:整理最全的 python 之markdown与HTML的互转的几个模块_zhaojiafu的博客-CSDN博客】
参考程序:csdn文章转换为markdown格式 -CSDN下载
修改后
import re
import parsel
import html2text
import requests
class CSDN():
def __init__(self):
self.url = "https://blog.csdn.net/qq_34842671/article/details/86062171"
def spider_csdn(self):
title_url = self.url
if not title_url:
print("请输入网址")
return
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52"
}
html = requests.get(url=title_url, headers=head).text
page = parsel.Selector(html)
title = page.css(".title-article::text").get()
res = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]")
restr = ''
res.sub(restr, title)
content = page.css("article").get()
content = re.sub("<a.*?a>", "", content)
content = re.sub("<br>", "", content)
texts = html2text.html2text(content)
with open(title + ".md", mode="w", encoding="utf-8") as f:
f.write("#" + title)
f.write(texts)
if __name__ == '__main__':
csdn = CSDN()
csdn.spider_csdn()
未精简
import re
import parsel
import tomd
import html2text
import requests
class CSDN():
def __init__(self):
self.url="https://blog.csdn.net/Jruo911/article/details/116745413"
def spider_csdn(self):
title_url=self.url
if not title_url:
return
head={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52"
}
html=requests.get(url=title_url,headers=head).text
page=parsel.Selector(html)
title=page.css(".title-article::text").get()
res = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]")
restr = ''
res.sub(restr, title)
content=page.css("article").get()
content=re.sub("<a.*?a>","",content)
content = re.sub("<br>", "", content)
texts=html2text.html2text(content)
with open(title+".md",mode="w",encoding="utf-8") as f:
f.write("#"+title)
f.write(texts)
if __name__ == '__main__':
csdn=CSDN()
csdn.spider_csdn()
|