首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【2024-完整版】python爬虫 批量查询自己所有CSDN文章的质量分:附整个实现流程

  • 25-02-18 14:00
  • 2128
  • 12559
blog.csdn.net

【2024】批量查询CSDN文章质量分

  • 写在最前面
  • 一、分析获取步骤
  • 二、获取文章列表
    • 1. 前期准备
    • 2. 获取文章的接口
    • 3. 接口测试(更新重点)
  • 三、查询质量分
    • 1. 前期准备
    • 2. 获取文章的接口
    • 3. 接口测试
  • 四、python代码实现
    • 1. 分步实现
    • 2. 批量获取文章信息
    • 3. 从excel中读取文章url,查询质量分,再将质量分添加到excel
    • 4. 全部代码


请添加图片描述

?你好呀!我是 是Yu欸
? 2024每日百字篆刻时光,感谢你的陪伴与支持 ~
? 欢迎一起踏上探险之旅,挖掘无限可能,共同成长!

前些天发现了一个人工智能学习网站,内容深入浅出、易于理解。如果对人工智能感兴趣,不妨点击查看。

写在最前面

之前的代码一直报错521,不清楚什么原因
因此重新分析整个过程,并对代码进行更新

结果如图

在这里插入图片描述

参考:
批量获取CSDN文章对文章质量分进行检测,有助于优化文章质量
【python】我用python写了一个可以批量查询文章质量分的小项目(纯python、flask+html、打包成exe文件)

一、分析获取步骤

  1. 获取博主的所有文章,并且拿到对应的url地址。(需要分析接口)
  2. 获取到url地址,我们需要使用官方查询质量分网页的接口进行请求。(需要分析接口)
  3. 接口分析完成后,我们就可以按照我们的需求进行代码编写了。

二、获取文章列表

1. 前期准备

浏览器访问需要获取文章的博主首页地址,并且打开开发者工具快捷键F12

然后点击网络选项,我们在刷新页面可以看到发送的请求地址。

然后我们选择XHR过滤掉我们不需要看到请求,但是这里面也没有我们需要的请求,但是没关系,我们只要想一下什么情况下会发送请求获取文章呢?答案就是下滑底部后,会重新发送请求获取新的文章并且渲染到页面。

点击删除请求这样我们下拉就可以清晰看到请求的接口数据

发现就是该接口发送的请求获取文章数据

在这里插入图片描述

2. 获取文章的接口

我们主要还是研究获取文章的接口
看请求的 url,是一个 GET 请求。

在这里插入图片描述

请求URL:
https://blog.csdn.net/community/home-api/v1/get-business-list

https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=blog&orderby=&noMore=false&year=&month=&username=wtyuong

这个接口也比较简单只需要携带4个参数:

  • 页码:page 第几页
  • 页数:size 页码展示的条数
  • 用户名称:username 需要查询的博主名(csdn id)
  • 业务类型:businessType 默认使用 blog 这个类型对应

分析响应体:可以返回每篇文章的地址、阅读量、评论量等数据。

['title', 'url', 'postTime', 'viewCount', 'collectCount', 'diggCount', 'commentCount']
['文章标题', 'URL', '发布时间', '阅读量', '收藏量', '点赞量', '评论量']
  • 1
  • 2

在这里插入图片描述

3. 接口测试(更新重点)

用ApiPost这个软件来进行接口测试

发现实际上,如果只发送url是会报错的,提示:请进行安全验证

在这里插入图片描述

服务器要求进行“安全验证”以继续访问。这通常是网站的防爬机制之一,用于识别和阻止自动化的访问尝试。面对这种情况,有几个可能的解决方案:

  1. 用户代理(User-Agent):确保你的请求头中包含了一个合理的用户代理(User-Agent)字符串。有些网站会检查这个字段来判断请求是否来自真实的浏览器用户。尝试使用常见浏览器的用户代理字符串。

  2. Cookies:某些网站要求请求携带有效的cookies来通过安全验证。你可以先手动访问该网站,通过浏览器获取到有效的cookies,并在你的爬虫请求中携带这些cookies。

  3. 处理JavaScript挑战:如果网站使用JavaScript生成动态内容或执行安全验证,你可能需要使用Selenium或Puppeteer这类工具,它们可以模拟真实的浏览器环境,执行JavaScript代码,并处理复杂的交互。

  4. 验证码识别:如果需要验证码验证,你可能需要集成验证码识别服务(如Google reCAPTCHA解决方案)或使用OCR(光学字符识别)技术尝试自动识别和填写验证码,虽然这可能面临法律和道德问题。

  5. 频率限制:确保你的请求频率不要太高,高频率的请求更容易触发网站的安全防护机制。尝试降低请求频率,或者在连续的请求之间增加延时。

经过测试,请求头只需要包括Cookies、Referer参数即可。

在这里插入图片描述

nice!

关于如何获取cookie:

在这里插入图片描述

三、查询质量分

流程和上述一样

1. 前期准备

先去质量查询地址:https://www.csdn.net/qc

在这里插入图片描述

2. 获取文章的接口

输入任意一篇文章地址进行查询,同时检查页面,在Network选项下即可看到调用的API的请求地址、请求方法、请求头、请求体等内容:

看请求的 url,是一个 POST 请求。

https://bizapi.csdn.net/trends/api/v1/get-article-score

在这里插入图片描述

POST 请求携带参数是 url。

在这里插入图片描述

我们得到的响应数据:文章id、分数、消息、发布时间。

在这里插入图片描述

3. 接口测试

请求头里面很多参数是不需要的,我们用ApiPost这个软件来测试哪些是必要参数。

需要注意的是请求体的类型是form-data类型

在这里插入图片描述

经过测试,请求头只需要下面这几个参数即可。

请求头分析
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
Accept :application/json, text/plain, /

在这里插入图片描述

响应体分析:

  • score:文章的分数
  • message:给出的建议

在这里插入图片描述

四、python代码实现

1. 分步实现

为了便于理解,把程序分为2个部分:

  • 批量获取文章信息,保存为excel文件;
  • 从excel中读取文章url,查询质量分,再将质量分添加到excel。

2. 批量获取文章信息

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

    def get_articles(self):
        url = "https://blog.csdn.net/community/home-api/v1/get-business-list"
        params = {
            "page": {self.page},
            "size": {self.size},
            "businessType": "blog",
            "username": {self.username}
        }
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
            'Cookie': self.cookies,  # Setting the cookies string directly in headers
            'Referer': self.Referer
        }

        try:
            response = requests.get(url, params=params, headers=headers)
            response.raise_for_status()  # Raises an HTTPError if the response status code is 4XX or 5XX
            data = response.json()
            return data.get('data', {}).get('list', [])
        except requests.exceptions.HTTPError as e:
            print(f"HTTP错误: {e.response.status_code} {e.response.reason}")
        except requests.exceptions.RequestException as e:
            print(f"请求异常: {e}")
        except json.JSONDecodeError:
            print("解析JSON失败")
        return []
    
    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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

在这里插入图片描述

3. 从excel中读取文章url,查询质量分,再将质量分添加到excel

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"
        # TODO: Replace with your actual headers
        headers = {
            "Accept": "application/json, text/plain, */*",
            "X-Ca-Key": "203930474",
            "X-Ca-Nonce": "b35e1821-05c2-458d-adae-3b720bb15fdf",
            "X-Ca-Signature": "gjeSiKTRCh8aDv0UwThIVRITc/JtGJkgkZoLVeA6sWo=",
            "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce",
            "X-Ca-Signed-Content-Type": "multipart/form-data",
        }
        data = {"url": article_url}
        try:
            response = requests.post(url, headers=headers, data=data)
            response.raise_for_status()  # This will raise an error for bad responses
            return response.json().get('data', {}).get('score', 'Score not found')
        except requests.RequestException as e:
            print(f"Request failed: {e}")
            return "Error fetching score"

    def get_scores_from_excel(self):
        df = pd.read_excel(self.filepath)
        urls = df['URL'].tolist()
        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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

4. 全部代码

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

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

    def get_articles(self):
        url = "https://blog.csdn.net/community/home-api/v1/get-business-list"
        params = {
            "page": {self.page},
            "size": {self.size},
            "businessType": "blog",
            "username": {self.username}
        }
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
            'Cookie': self.cookies,  # Setting the cookies string directly in headers
            'Referer': self.Referer
        }

        try:
            response = requests.get(url, params=params, headers=headers)
            response.raise_for_status()  # Raises an HTTPError if the response status code is 4XX or 5XX
            data = response.json()
            return data.get('data', {}).get('list', [])
        except requests.exceptions.HTTPError as e:
            print(f"HTTP错误: {e.response.status_code} {e.response.reason}")
        except requests.exceptions.RequestException as e:
            print(f"请求异常: {e}")
        except json.JSONDecodeError:
            print("解析JSON失败")
        return []
    
    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"
        # TODO: Replace with your actual headers
        headers = {
            "Accept": "application/json, text/plain, */*",
            "X-Ca-Key": "203930474",
            "X-Ca-Nonce": "b35e1821-05c2-458d-adae-3b720bb15fdf",
            "X-Ca-Signature": "gjeSiKTRCh8aDv0UwThIVRITc/JtGJkgkZoLVeA6sWo=",
            "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce",
            "X-Ca-Signed-Content-Type": "multipart/form-data",
        }
        data = {"url": article_url}
        try:
            response = requests.post(url, headers=headers, data=data)
            response.raise_for_status()  # This will raise an error for bad responses
            return response.json().get('data', {}).get('score', 'Score not found')
        except requests.RequestException as e:
            print(f"Request failed: {e}")
            return "Error fetching score"

    def get_scores_from_excel(self):
        df = pd.read_excel(self.filepath)
        urls = df['URL'].tolist()
        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 = 10     #已发文章总数量
    # TODO:调整为你自己的cookies,Referer,CSDNid, headers
    cookies = 'uuid_tt_dd=10'  # Simplified for brevity
    Referer = 'https://blog.csdn.net/WTYuong?type=blog'
    CSDNid = 'WTYuong'
    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(CSDNid, cookies, Referer, index, 100, filename)  # Replace with your username
        exporter.export_to_excel()
        # 批量获取质量分
        score = ArticleScores(filename)
        score.write_scores_to_excel()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
赠书活动 | 粉丝福利 | 商务合作
微信名片
注:本文转载自blog.csdn.net的是Yu欸的文章"https://blog.csdn.net/wtyuong/article/details/136683702"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top