利用python同步Github上的题解
前言
博客里有一篇Leetcode题解,想着要同步更新,没有思考过程,至少想把代码贴出来;结果自己很懒,并没有随声更新;但是自己一定会上传题解到github上,所以就写了一段代码将github上leetcode题解的链接爬取下来。
同步题解
这个代码不难,无非就是有正则表达式,所以不多废话,爬取部分的代码如下:
# -*- coding: utf-8 -*-
import requests
import os
import re
base_url="https://github.com"
headers={
'user-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
}
def crawl():
r=requests.get("https://github.com/YunLambert/C-plus-Algorithm_Learning/tree/master/Leetcode题解/Leetcode_Solutions/Leetcode_Solutions",headers=headers)
title_pattern=re.compile('/YunLambert/C-plus-Algorithm_Learning/blob/master/Leetcode%E9%A2%98%E8%A7%A3/Leetcode_Solutions/Leetcode_Solutions/([A-Za-z0-9_]*).cpp')
titles=re.findall(title_pattern,r.text)
#print(titles)
link_pattern=re.compile('.*.href="(.*.cpp)"')
links=re.findall(link_pattern,r.text)
#print(links)
return titles,links
def rename(problem_title,problem_url):
for (title,url) in zip(problem_title,problem_url):
s='['+title+']'+'('+base_url+url+')'
save(s)
def save(s):
with open('save.txt','a+') as f:
f.write(s+'\n')
if __name__=="__main__":
a,b=crawl()
rename(a,b)
最后是强行手动转成markdown格式 s='['+title+']'+'('+base_url+url+')'
,然后可以导出txt文件,也可以直接写进自己的博客里,都是ok的。
最后出现的结果是这样的:
这样只要自己的github上有题解在更新,自己就能通过这段代码将题目和链接爬取下来,写进自己的博客里,算是辅助脚本吧,继续刷题了........
作者:YunLambert
-------------------------------------------
个性签名:一名会音乐、爱健身的不合格程序员
可以Follow博主的Github哦(っ•̀ω•́)っ✎⁾⁾