通过Python+Selenium获取我的所有文章质量分

这篇具有很好参考价值的文章主要介绍了通过Python+Selenium获取我的所有文章质量分。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

大家好,我是空空star,本篇给大家分享一下《通过Python+Selenium获取我的所有文章质量分》

一、背景

目前在质量分查询界面只能一篇文章一篇文章查,如果我们想快速知道我们哪些文章质量分不是高质量,手动一篇一篇查太慢,借助Selenium代替手动,快速查出我们所有文章的质量分,然后再保存成excle。这样我们就可以很直观得看到哪些文章不是80分以上。

当然,也可以直接通过质量分接口去查询,接口做了HMAC认证,感兴趣的同学可以尝试。

二、设计

  1. 查询我的所有文章数量;
  2. 根据文章数量计算翻页数;
  3. 获取每页的文章url;
  4. 遍历页数,将每页的文章url添加到数组article_list中;
  5. 遍历article_list,通过Selenium去查质量分相关信息;
  6. 将查到的信息列分别放到对应的数组中;
  7. 通过pandas将质量分相关列信息写入excle。

三、环境准备

浏览器:本篇使用的是Chrome
Chrome驱动版本:110.0.5481.77
Python版本:Python3.8
selenium版本: 4.8.2
Selenium基础篇之环境准备

四、开发

1.查询文章数量

1.1请求url

通过Python+Selenium获取我的所有文章质量分,Selenium,python,selenium,开发语言

1.2响应结果

通过Python+Selenium获取我的所有文章质量分,Selenium,python,selenium,开发语言

1.3代码

def get_article_total(username):
    url = 'https://blog.csdn.net/community/home-api/v1/get-tab-total'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763'
    }
    params = {'username': username}
    response = requests.get(url, params=params, headers=headers)
    return response.json()['data']['blog']

2.获取每页文章列表

2.1请求url

通过Python+Selenium获取我的所有文章质量分,Selenium,python,selenium,开发语言

2.2响应结果

通过Python+Selenium获取我的所有文章质量分,Selenium,python,selenium,开发语言

通过请求参数,可以看到每次最多返回20个,那么翻页数就是:
max_page = math.ceil(blog_total/20)

3.获取所有文章url

3.1代码

def get_article_list(username):
    article_list = []
    url = 'https://blog.csdn.net/community/home-api/v1/get-business-list'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763'
    }
    blog_total = get_article_total(username)
    max_page = math.ceil(blog_total/20)
    for page in range(1, max_page+1):
        params = {'page': page, 'size': 20, 'businessType': 'blog', 'username': username}
        response = requests.get(url, params=params, headers=headers)
        for item in response.json()['data']['list']:
            article_list.append(item['url'])
    return article_list

4.保存excle方法

4.1代码

def save_excle(filename,post_time_list,title_list,score_list,remark_list,article_list):
    data = {'作者-发布时间': post_time_list, '文章标题': title_list, '质量分': score_list, '分数说明': remark_list, '文章url': article_list}
    df = pd.DataFrame(data)
    writer = pd.ExcelWriter(filename+'.xlsx')
    df.to_excel(writer, index=False)
    writer.save()

5.查询所有文章质量分并写入excle

5.1代码

def get_score(username):
    post_time_list = []
    article_list = get_article_list(username)
    title_list = []
    score_list = []
    remark_list = []
    cnt = 0
    for blog_url in article_list:
        cnt += 1
        driver.find_elements(By.CLASS_NAME, 'el-input__inner')[0].send_keys(blog_url)
        print(f'开始进行第{cnt}次查询')
        driver.find_elements(By.CLASS_NAME, 'trends-input-box-btn')[0].click()
        time.sleep(1)
        texts = driver.find_elements(By.TAG_NAME, 'p')
        title = driver.find_elements(By.TAG_NAME, 'span')[3].text
        post_time = driver.find_elements(By.TAG_NAME, 'span')[4].text
        post_time_list.append(post_time)
        title_list.append(title)
        score_list.append(texts[1].text)
        remark_list.append(texts[2].text)
    print('查询完毕!开始写入excle。')
    save_excle(username, post_time_list, title_list, score_list, remark_list, article_list)
    print('写入excle完毕!')

6.程序入口

只需要把username改成你的即可文章来源地址https://www.toymoban.com/news/detail-571311.html

6.1代码

if __name__ == '__main__':
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    driver = webdriver.Chrome(options=options)
    driver.get('https://www.csdn.net/qc')
    driver.maximize_window()
    get_score('weixin_38093452')
    driver.quit()

7.需要引入的模块

import math
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import requests
import pandas as pd

五、效果

通过Python+Selenium获取我的所有文章质量分,Selenium,python,selenium,开发语言

总结

到了这里,关于通过Python+Selenium获取我的所有文章质量分的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【python】使用Selenium获取(2023博客之星)的参赛文章

    2023博客之星活动已经过了半年之久,出于好奇,想看看目前为止到底有多少人参与了, 由于小助手每次只发单独赛道的, 因此无法窥其全貌,进行对比, 因此写了这个脚本,来分析一下, 看到结果之后, 很想放弃啊, 太卷了. 这一部分代码导入了所需的模块,其中包括selenium、json、ti

    2024年02月14日
    浏览(25)
  • python爬虫—selenium获取csdn质量分并用echarts可视化分析

    大家好,我是yma16,本文分享关于python自动化获取个人博客质量分并可视化。 该系列文章: python爬虫_基本数据类型 python爬虫_函数的使用 python爬虫_requests的使用

    2024年02月11日
    浏览(36)
  • python通过selenium获取输入框的文本值爬取编辑框内容

    以百度首页的输入框为例,当输入‘你好‘后,html中的value的值会变成‘你好’ 运行代码得到以下效果

    2024年02月04日
    浏览(41)
  • python通过selenium爬取网页信息,python获取浏览器请求内容,控制已经打开的浏览器

    背景:通过python中直接get或者urlopen打开一些有延迟加载数据的网页,会抓取不到部分信息。 1. 命令行打开chrome,并开启调试端口 (前提,找到chrome安装目录,找到chrome.exe所在路径,添加到环境变量中,例如我的是C:Program FilesGoogleChromeApplication) remote-debugging-port指定远程调试

    2024年02月16日
    浏览(55)
  • 【Python】所有文章传送门(持续更新...)

    Python 教程 【人生苦短,我学 Python】(1)初识 Python 【人生苦短,我学 Python】(2)Python 语言基础 【人生苦短,我学 Python】(3)Python 常用内置数据类型 I —— 数值数据类型(int、float、complex、bool) 【人生苦短,我学 Python】(4)Python 常用内置数据类型 II —— 序列数据类

    2024年02月20日
    浏览(34)
  • python爬虫_django+vue+echarts可视化查询所有CSDN用户质量分

    大家好,我是yma16,本文分享关于前后分离django+vue+echarts可视化查询CSDN用户质量分。 该系列文章: python爬虫_基本数据类型 python爬虫_函数的使用 python爬虫_requests的使用 python爬虫_selenuim可视化质量分 ⭐ 效果 项目部署在inscode上:https://yma16.inscode.cc/ 表格展示文章评分 echarts图

    2024年02月12日
    浏览(47)
  • 【python】我用python写了一个可以批量查询文章质量分的小项目(纯python、flask+html、打包成exe文件)

    web 效果预览: 先去质量查询地址:https://www.csdn.net/qc 输入任意一篇文章地址进行查询,同时检查页面,在Network选项下即可看到调用的API的请求地址、请求方法、请求头、请求体等内容: 请求头里面很多参数是不需要的,我们用 ApiPost 这个软件来测试哪些是必要参数。 经过

    2024年02月13日
    浏览(28)
  • python使用selenium以及selenium-wire做质量与性能检测

    python天生就是适合用来做爬虫,结合selenium真是如虎添翼; 1) 安装库 2)添加驱动,比如 chrome需要下载一个驱动,放到项目目录下或者python安装目录下,根据机器上对应的chrome版本进行下载。我是放在python3.exe的目录 下载地址: CNPM Binaries Mirror selenium功能比较强大,但是仍然

    2024年02月11日
    浏览(52)
  • python获取json的所有“键“

    1、明确概念 首先json对象是字符串。 在python中,虽然json对象和dict\\\"长相\\\"相差无几,但是区别在于json对象的键值使用了双引号,而dict使用单引号。 在json模块中, json.dumps(): 对数据进行编码。会将python对象(dict)转化为json对象 json.loads(): 对数据进行解码。将json对象转化为

    2024年02月11日
    浏览(21)
  • Python使用Selenium Webdriver爬取网页所有内容

    有时候,我们在用urllib或者requests库抓取页面时,得到的html源代码和浏览器中看到的不一样。这将是我们面临的一个非常常见的问题。现在网页越来越多地采用Ajax、前端模块化工具来构建,整个网页可能都是由JavaScript渲染出来的,也就是说原始的HTML代码可能就是一个空壳,

    2023年04月08日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包