前言
博客里有一篇Leetcode題解,想著要同步更新,沒有思考過程,至少想把代碼貼出來;結(jié)果自己很懶,并沒有隨聲更新;但是自己一定會上傳題解到github上,所以就寫了一段代碼將github上leetcode題解的鏈接爬取下來。
同步題解
這個代碼不難,無非就是有正則表達(dá)式,所以不多廢話,爬取部分的代碼如下:
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)
最后是強行手動轉(zhuǎn)成markdown格式s='['+title+']'+'('+base_url+url+')',然后可以導(dǎo)出txt文件,也可以直接寫進自己的博客里,都是ok的。
最后出現(xiàn)的結(jié)果是這樣的:

2018-11-08_14-40-15.png
這樣只要自己的github上有題解在更新,自己就能通過這段代碼將題目和鏈接爬取下來,寫進自己的博客里,算是輔助腳本吧,繼續(xù)刷題了........