首页 > 其他分享 >再见,洛谷博客!——下载洛谷博客文章

再见,洛谷博客!——下载洛谷博客文章

时间:2023-07-20 12:55:34浏览次数:41  
标签:再见 bid 博客 blog json result print 洛谷 response

posted on 2022-11-06 10:58:17 | under 学术 | source

前置知识

单组询问

F12

// copy the code of blogs
fetch('/api/blog/detail/' + BlogGlobals.blogID).then(res => res.json()).then(res => console.log(res.data.Content))

多次询问下载 markdown

# fetcher.py
import os
import json
import time
import requests

url="https://www.luogu.com.cn"
print("Warning: When the program is running, don't log out your luogu account...")
uid=input("Please input your user id...")
client=input("Please input your client id...")
savePath=input("Where do you want to save? (e.g. ./blogs/)")
# os.mkdir(savePath)

userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0"
headers={
    "User-Agent": userAgent,
    "cookie": "_uid="+uid+";__client_id="+client
}

restCount=0

def fetchBlogContent(bid):
    global restCount
    restCount+=1
    if restCount%10==0:
        print("Please wait for 10 seconds...")
        time.sleep(10)
    response=requests.get(url+"/api/blog/detail/"+str(bid),headers=headers)
    response.encoding="utf-8"
    result=json.loads(response.text)
    # print(result)
    # print(json.dumps(result,sort_keys=True, indent=4, separators=(',', ': ')))
    if result["status"] == 200:
        result=result["data"]
        return {
            "source": url+"/blog/_post/"+str(bid),
            "content": result["Content"],
            "identifier": result["Identifier"],
            "postTime": result["PostTime"],
            "title": result["Title"],
            "type": result["Type"]
        }
    else:
        print("Error: Cannot catch blog {%d}, result:"%bid)
        print(result)
        return {}
    
def saveBlogContent(bid):
    result=fetchBlogContent(bid)
    if result == {}:
        return 
    postTime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(result["postTime"]))
    with open(savePath+"%s.md"%result["identifier"],mode="w",encoding="utf-8") as file:
        file.write("# %s\n"%result["title"])
        file.write("posted on %s | under %s | [source](%s)\n\n"%(postTime,result["type"],result["source"]))
        file.write(result["content"])
        file.write("\n")

def getBlogLists(uid):
    response=requests.get(url+"/api/blog/userBlogs",headers=headers)
    response.encoding="utf-8"
    result=json.loads(response.text)["blogs"]
    # print(json.dumps(blogs,sort_keys=True, indent=4, separators=(',', ': ')))
    lists=[]
    pageNumber=result["count"]//result["perPage"]+1
    for i in range(1,pageNumber+1):
        response=requests.get(url+"/api/blog/userBlogs?page="+str(i),headers=headers)
        response.encoding="utf-8"
        result=json.loads(response.text)["blogs"]
        for blog in result["result"]:
            lists.append(blog["id"])
    return lists

print("Now start fetching blogs of user %s..."%uid)
for bid in getBlogLists(uid):
    saveBlogContent(bid)
    print("Save blog %d successfully..."%bid)
print("Blogs of user %s have already saved..."%uid)
print("Thank you for using my tool. Author: luogu@yukimianyan.")

标签:再见,bid,博客,blog,json,result,print,洛谷,response
From: https://www.cnblogs.com/caijianhong/p/16863811.html

相关文章

  • 洛谷 P9020 - [USACO23JAN] Mana Collection P
    显然,每个法力池最终能收集到的法力只与这个法力池最终被收集到的时间有关。对于一组询问\((s,e)\),假设我们经过了\(k\)个法力池,我们钦定最终被收集到的时间从后到前分别是\(e=a_1,a_2,\cdots,a_k\),那么最大法力值为\(\sum\limits_{i=1}^kc_{a_i}·\sum\limits_{j=2}^i(s-dis......
  • Gridea 搭建的博客无法加载 CSS、网页图标、头像配置
    前阵子发现了用来写静态博客的软件Gridea,配置使用后发现,网页和软件预览的完全不同,打开DevTools一看,CSS压根就没加载出来,同样的,其他静态资源也都没有正确加载。目前的Gridea版本号是0.0.3仔细观察后发现,多了一个/,如图再回到软件配置上,可能是因为多打了这一个尾部的/,导......
  • 第一次开通博客园
    第一次开通自己的博客园,主要是想在暑假期间记录一下自己的学习过程,我会把学习的知识总结发表于我的博客,还可以写一些日常的感悟和心情。总之,以后的各种学习我都会总结发表,通过这种形式监督自己,一点一点的进步。qwq  ......
  • 关于我的第一篇博客
    你好,欢迎来到我的博客目录1.我为什么要写博客2.我要用博客来记录什么3.对于未来的展望与自省1.我为什么要写博客  这是一件我想了很久但是却一直没做的事情,作为一名计算机专业的在校大学生,无论在学校学习的专业知识亦或者自己在哔哩哔哩自学,接触到的专业知识都是海量的,加......
  • Django-4.2博客开发教程:需求分析并确定数据表(四)
    前三步已经完成了一个初步流程,从创建项目》应用》数据迁移》访问首页。以下是我整理的基本流程,接下来一步一步完成整个项目。 1.我们的需求: 博客的功能主要分为:网站首页、文章分类、文章内容、图片、文章推荐、文章排行、热门推荐、文章搜索 等~1、网站首页:首页是整个网站......
  • 洛谷 P1122 最大子树和 题解
    一道入门的树形DP。首先我们对于数据进行有序化处理,这便于我们利用数据结构特点(可排序性)来发觉数据性质(有序、单调、子问题等等性质),以便于后续的转化、推理和处理。有序化可以“转化和创造”性质首先将视角从无根树切换为有根树,这样我们就可以得到一个带有最优子结构、无后效性......
  • 洛谷 P2458 [SDOI2006] 保安站岗 - 树形DP
    P2458保安站岗思路:树形DP三个状态:dp[i][0]:节点i位置放保安的最小花费dp[i][1]:节点i位置不放保安,但被子节点的保安看守dp[i][2]:节点i位置不放保安,但被父节点的保安看守状态转移:dp[i][0]:节点i位置放保安,那么它可以合并子节点任何状态的最小值;dp[i][1]:节点i位......
  • 洛谷 P8923 -『MdOI R5』Many Minimizations
    怎么ARC还能撞题的?只能说Kubic牛逼。首先显然没法保序回归。考虑用类似于凸壳优化DP的做法解决原问题(也就是P4331):设\(dp_{i,j}\)表示考虑前\(i\)位,\(x_i=j\)的最小代价,显然有\(dp_{i,j}=\min_{k\lej}\{dp_{i-1,k}+|j-a_i|\}\)\(dp\)值显然是一个折线,用堆维护斜......
  • ### 关于博客学院小结
    关于博客学院小结测试博客园联合VScode1.1开始1.2继续1.3结束试试2.1开了2.3好的hhahhaapublicclass{publicstaticvoidmain(String[]args){System.out.println("HelloWorld");}}想法......
  • 第一个博客
    一级标题二级标题字体helloworldhelloworldhelloworldhelloworldhelloworld引用方法英文大于号+空格asdk分割线电饭锅---(三个减号)或者(三个*)图片方法![图片名字]再加英文括号()超链接点击跳转狂神方法[超链接名字]再加英文括号()列表1方法序号+.加空......