CSDN 品質ページングのクエリ操作とコード

品質スコアのクエリ Web サイト

品質スコアのクエリ Web サイト

品質スコアリクエストAPI

POST请求地址:https://bizapi.csdn.net/trends/api/v1/get-article-score

参考サイト:【python】記事の品質スコアをバッチでクエリできる小さなプロジェクトをPythonで書いてみました(pure python, flask+html, packaged into exe files) 参考サイト: How to query thequality sinners of your own CSDN blogバッチ

応答ヘッダー情報:

例としてクエリ https://blog.csdn.net/Medlar_CN/article/details/132229859 を取り上げます。
ここに画像の説明を挿入
ブラウザで F12 を押して Web ページ情報を表示し、[ネットワーク] タブを選択すると、次の応答ヘッダーが表示されます。 information:ここに画像の説明を挿入
主な情報は次のとおりです:
authority:
bizapi.csdn.net
:method:
POST
:path:
/trends/api/v1/get-article-score
:scheme:
https
Accept:
application/json, text/plain, /
Accept-Encoding:
gzip、deflate、br
Accept-Language:
zh-CN,zh;q=0.9
Content-Length:
191
Content-Type:
multipart/form-data;boundary=----WebKitFormBoundaryCfQFnl0pJrpx5ZKk
Cookie: Omit
Origin:
https ://www.csdn.net
リファラー :
https://www.csdn.net/qc
Sec-Ch-Ua:
"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115" Sec-Ch-Ua-Mobile:
?
0
Sec-Ch-Ua-Platform:
「Windows」
Sec-Fetch-Dest:

Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
同じサイト
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Gecko のような KHTML) Chrome/115.0.0.0 Safari/537.36
X-Ca-Key:省略
X-Ca-Nonce:省略
X-Ca-Signature:省略
X-Ca-Signature-Headers:
x-ca- key,x-ca-nonce
X-Ca-Signed-Content-Type:
multipart/form-data

品質コードを一括取得(リファレンスコードに合わせて修正)

import urllib.request
import json
import pandas as pd
from openpyxl import Workbook, load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import math

# 批量获取文章信息并保存到excel
class CSDNArticleExporter:
    def __init__(self, username, page, size, filename):
        self.username = username
        self.size = size
        self.filename = filename
        self.page = page

    def get_articles(self):
        url = f"https://blog.csdn.net/community/home-api/v1/get-business-list?page={
      
      self.page}&size={
      
      self.size}&businessType=blog&orderby=&noMore=false&year=&month=&username={
      
      self.username}"
        with urllib.request.urlopen(url) as response:
            data = json.loads(response.read().decode())
        return data['data']['list']

    def export_to_excel(self):
        df = pd.DataFrame(self.get_articles())
        df = df[['title', 'url', 'postTime', 'viewCount', 'collectCount', 'diggCount', 'commentCount']]
        df.columns = ['文章标题', 'URL', '发布时间', '阅读量', '收藏量', '点赞量', '评论量']
        # df.to_excel(self.filename)
        # 下面的代码会让excel每列都是合适的列宽,如达到最佳阅读效果
        # 你只用上面的保存也是可以的
        # Create a new workbook and select the active sheet
        wb = Workbook()
        sheet = wb.active
        # Write DataFrame to sheet
        for r in dataframe_to_rows(df, index=False, header=True):
            sheet.append(r)
        # Iterate over the columns and set column width to the max length in each column
        for column in sheet.columns:
            max_length = 0
            column = [cell for cell in column]
            for cell in column:
                try:
                    if len(str(cell.value)) > max_length:
                        max_length = len(cell.value)
                except:
                    pass
            adjusted_width = (max_length + 5)
            sheet.column_dimensions[column[0].column_letter].width = adjusted_width
        # Save the workbook
        wb.save(self.filename)


# 批量查询质量分
class ArticleScores:
    def __init__(self, filepath):
        self.filepath = filepath

    @staticmethod
    def get_article_score(article_url):
        url = "https://bizapi.csdn.net/trends/api/v1/get-article-score"
        headers = {
    
    
            "Accept": "application/json, text/plain, */*",
            "X-Ca-Key": "填自己的",
            "X-Ca-Nonce": "填自己的",
            "X-Ca-Signature": "填自己的",
            "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce",
            "X-Ca-Signed-Content-Type": "multipart/form-data",
        }
        data = urllib.parse.urlencode({
    
    "url": article_url}).encode()
        req = urllib.request.Request(url, data=data, headers=headers)
        with urllib.request.urlopen(req) as response:
            return json.loads(response.read().decode())['data']['score']

    def get_scores_from_excel(self):
        # Read the Excel file
        df = pd.read_excel(self.filepath)
        # Get the 'URL' column
        urls = df['URL']
        # Get the score for each URL
        scores = [self.get_article_score(url) for url in urls]
        return scores

    def write_scores_to_excel(self):
        df = pd.read_excel(self.filepath)
        df['质量分'] = self.get_scores_from_excel()
        df.to_excel(self.filepath,index=False)


if __name__ == '__main__':
    total = 212     #已发文章总数量
    t_index = math.ceil(total/100)+1 #向上取整,半闭半开区间,开区间+1。
    # 获取文章信息
    # CSDNArticleExporter("待查询用户名", 2(分页数量,按总文章数量/100所得的分页数),总文章数量仅为设置为全部可见的文章总数。
    # 100(最大单次查询文章数量不大于100), 'score1.xlsx'(待保存数据的文件,需要和下面的一致))
    for index in range(1,t_index): #文章总数
        filename = "score"+str(index)+".xlsx"
        exporter = CSDNArticleExporter("Medlar_CN", index, 100, filename)  # Replace with your username
        exporter.export_to_excel()
        # 批量获取质量分
        score = ArticleScores(filename)
        score.write_scores_to_excel()

パラメータの説明:

total = 212 #公開記事の総数
t_index = math.ceil(total/100)+1 #切り上げ、半クローズおよび半オープン区間、オープン区間 +1。
記事情報の取得
CSDNArticleExporter("照会するユーザー名", 2(ページ数、記事総数/100で計算)、記事総数は全表示に設定した記事の総数のみ。100(単一最大)クエリ
記事の数値は 100 以下です)、「score1.xlsx」(データを保存するファイルは以下と一致している必要があります))

出力ファイルの例

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/Medlar_CN/article/details/132277930