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里面美观,请感兴趣的博友改进吧。