cnblogs_client

Posted andy9468

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cnblogs_client相关的知识,希望对你有一定的参考价值。

import requests
import json
from openpyxl import Workbook

url = r‘https://i-beta.cnblogs.com/api/posts/list?p=18&cid=&t=1&cfg=0‘
headers = {
    ‘method‘: ‘GET‘,
    ‘accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9‘,
    ‘authority‘: ‘i-beta.cnblogs.com‘,
    ‘accept-language‘: ‘zh-CN,zh;q=0.9‘,
    ‘cache-control‘: ‘max-age=0‘,
    ‘sec-fetch-user‘: ‘?1‘,
    ‘path‘: ‘/api/posts/list?p=18&cid=&t=1&cfg=0‘,
    ‘sec-fetch-mode‘: ‘navigate‘,
    ‘sec-fetch-dest‘: ‘document‘,
    ‘upgrade-insecure-requests‘: ‘1‘,
    ‘user-agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36‘,
    ‘accept-encoding‘: ‘gzip, deflate, br‘,
    ‘scheme‘: ‘https‘,
    ‘sec-fetch-site‘: ‘none‘,
    ‘cookie‘: 0}

# 发起get请求
response = requests.get(url, headers=headers, verify=True)
print(response.status_code)
html_data = response.content.decode()
data_dict = json.loads(html_data)
post_list = data_dict["postList"]
print(post_list)

# 将数据存入excel
save_file = "1.xls"  # 存入文件名称
sheet_name = "admin"  # 工作簿名称
info_result = []  # 写入表格的数据
# 写入首行
info = ["title", "viewCount", "url", "comment_count", "datePublished", "dateUpdated"]  # 首行内容
info_result.append(info)
for item in post_list:
    title = item[‘title‘]
    viewCount = item[‘viewCount‘]
    url = "https:%s" % item[‘url‘]
    comment_count = item[‘feedBackCount‘]
    datePublished = item[‘datePublished‘]
    dateUpdated = item[‘dateUpdated‘]
    print(title)
    print(viewCount)
    print(url)
    print(comment_count)
    print("首发日期:", datePublished)
    print("修改日期:", dateUpdated)
    print()

    # info = [‘a‘, ‘b‘, ‘c‘]  # 每行的内容
    info = [title, viewCount, url, comment_count, datePublished, dateUpdated]  # 每行的内容
    info_result.append(info)

# 写入Excel表格
wb = Workbook()
ws1 = wb.active
ws1.title = sheet_name  # sheet名称
for row in info_result:
    ws1.append(row)
wb.save(save_file)  # Excel文件名称,保存文件

  

以上是关于cnblogs_client的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数