获取CSDN文章内容并转换为markdown文本的python

所属分类: 脚本专栏 / python 阅读数: 1878
收藏 0 赞 0 分享

自己写的小工具,可以直接获取csdn文章并转换为markdown格式

效果图

核心代码

from PySide2.QtWidgets import QApplication,QMainWindow,QPushButton,QPlainTextEdit,QMessageBox
import re
import parsel
import tomd
import requests
class CSDN():
  def __init__(self):
    self.windows = QMainWindow()
    self.windows.resize(450, 300)
    self.windows.setWindowTitle("轻松获取csdn文章--by tansty")
    self.setup_ui()
    self.set_connect()
  def set_connect(self):
    #设置建立联系
    self.button.clicked.connect(self.spider_csdn)
  def setup_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)
  def spider_csdn(self):
    # 目标文章的链接
    title_url=self.text.toPlainText()
    MessageBox = QMessageBox(self.windows)
    if not title_url:
      MessageBox.critical(self.windows, "错误", "请输入网址")
      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=tomd.Tomd(content).markdown
    #转换为markdown 文件
    with open(title+".md",mode="w",encoding="utf-8") as f:
      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

更多精彩内容其他人还在看

用Python实现服务器中只重载被修改的进程的方法

这篇文章主要介绍了用Python实现服务器中只重载被修改的进程的方法,包括用watchdog来检测文件的变化等,实现起来充分体现了Python作为动态语言的灵活性,强烈推荐!需要的朋友可以参考下
收藏 0 赞 0 分享

python使用mailbox打印电子邮件的方法

这篇文章主要介绍了python使用mailbox打印电子邮件的方法,涉及Python打印电子邮件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python中随机函数random用法实例

这篇文章主要介绍了python中随机函数random用法,实例分析了random函数的相关使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python模拟enum枚举类型的方法小结

这篇文章主要介绍了python模拟enum枚举类型的方法,实例总结了python模拟enum枚举类型的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python遍历数组的方法小结

这篇文章主要介绍了python遍历数组的方法,实例总结了两种Python遍历数组的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现指定字符串补全空格的方法

这篇文章主要介绍了python实现指定字符串补全空格的方法,涉及Python中rjust,ljust和center方法的使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现同时给多个变量赋值的方法

这篇文章主要介绍了python实现同时给多个变量赋值的方法,涉及Python中变量赋值的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中assert用法实例分析

这篇文章主要介绍了python中assert用法,实例分析了assert的功能及相关使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中pass语句用法实例分析

这篇文章主要介绍了python中pass语句用法,对比C++程序实例分析了pass语句的使用方法,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现的简单文本类游戏实例

这篇文章主要介绍了python实现的简单文本类游戏,以两个实例形式分析了python操作文本与字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多