获取CSDN文章内容并转换为markdown文本的python
自己写的小工具,可以直接获取csdn文章并转换为markdown格式
效果图
核心代码
fromPySide2.QtWidgetsimportQApplication,QMainWindow,QPushButton,QPlainTextEdit,QMessageBox
importre
importparsel
importtomd
importrequests
classCSDN():
def__init__(self):
self.windows=QMainWindow()
self.windows.resize(450,300)
self.windows.setWindowTitle("轻松获取csdn文章--bytansty")
self.setup_ui()
self.set_connect()
defset_connect(self):
#设置建立联系
self.button.clicked.connect(self.spider_csdn)
defsetup_ui(self):
#设置ui界面的建立
self.button=QPushButton(self.windows)
self.button.resize(100,100)
self.button.move(150,150)
self.button.setText("获取文章")
self.text=QPlainTextEdit(self.windows)
self.text.setPlaceholderText("请输入需要获取文章的链接")
self.text.resize(450,100)
defspider_csdn(self):
#目标文章的链接
title_url=self.text.toPlainText()
MessageBox=QMessageBox(self.windows)
ifnottitle_url:
MessageBox.critical(self.windows,"错误","请输入网址")
return
head={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.105Safari/537.36Edg/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("","",content)
content=re.sub("
","",content)
texts=tomd.Tomd(content).markdown
#转换为markdown文件
withopen(title+".md",mode="w",encoding="utf-8")asf:
f.write("#"+title)
f.write(texts)
MessageBox.information(self.windows,"正确","获取文章完成")
if__name__=='__main__':
app=QApplication()
csdn=CSDN()
csdn.windows.show()
app.exec_()
文件打包下载链接:https://pan.baidu.com/s/1R6RcrDagwf1vWzmRCBja4w提取码:ug6n
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。