将jupyter notebook转换成csdn博客


jupyter notebook得到简洁直观的代码与执行结果,但如果希望发布成博客,通常需要经过github中转,比较费时费力。以下采用爬取本地网页的方法,快速把jupyter notebook的内容发布为csdn博客。

第一步,将jupyter notebook内容download as html。

这里假设html为 1.html。

第二步,在PyCharm中run以下脚本

from bs4 import BeautifulSoup
# 本地保存的jupyter notebook网页
# 仅支持code和png
htmlfile = open("E:/notes/1.html", 'r', encoding='utf-8')
html = htmlfile.read()
csdn=open("E:/notes/csdn.txt",'w',encoding='utf-8')
soup = BeautifulSoup(html, 'lxml')
toc = soup.select('head > title')
# 博客的目录
csdn.write("@[TOC](" + toc[0].get_text()+")\n")

result = soup.find_all('div', class_="cell border-box-sizing code_cell rendered")
for inout in result:
    ins = inout.find_all('div', class_="prompt input_prompt")
    csdn.write('%s%s%s' % ( '## ',ins[0].get_text(),'\n'))
    inbox = inout.find('div',class_=" highlight hl-ipython3")
    csdn.write("```"+'\n')
    csdn.write(inbox.get_text().strip()+'\n')
    csdn.write("```"+'\n')
    csdn.write("Output:" + '\n')
    outbox = inout.find_all('div',class_="output_area")
    csdn.write('```\n')
    for outs in outbox:
        out_pre = outs.get_text().strip()
        csdn.write(out_pre +'\n')
        outpng = outs('div', class_="output_png")
        png_list = []
        for pngs in outpng:
            outpng_pre = pngs.find('img').get('src')
            png_list.append(outpng_pre)
    csdn.write('```\n')
    if ( png_list != []):
        for apgn in png_list:
            csdn.write('<img src="{}">\n'.format(apgn))
    png_list = []
    print("----------------"+'\n')
csdn.close()

第三步,粘贴发布

打开csdn.txt,直接粘贴到csdn“写博客”编辑器的左侧编辑栏中,不用修改一个字即可发布。
目前的格式不如jupyter里面美观,请感兴趣的博友改进吧。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值