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文件名称,保存文件
原文:https://www.cnblogs.com/andy9468/p/12663809.html