【脚本】网页端微信读书书架中书籍详细信息

首先进入书架中的一个分组
在这里插入图片描述
然后在控制台输入如下代码

// 书架中的书籍链接
let shelf_arr = []; // 存储链接
let shelf_book = document.getElementsByClassName("shelfBook"); // 书籍
for (i = 0; i < shelf_book.length; i++) {
  shelf_arr.push(shelf_book[i].href.replace("reader", "bookDetail"));
}
console.log(shelf_arr);

在这里插入图片描述

// 排行榜书籍
let shelf_arr = []; // 存储链接
let shelf_book = document.getElementsByClassName("wr_bookList_item_link"); // 书籍
for (i = 0; i < shelf_book.length; i++) {
  shelf_arr.push(shelf_book[i].href);
}
console.log(shelf_arr);

在这里插入图片描述

复制到py脚本的shelf_arr数组中(需要安装requests和BeautifulSoup库)

# -*-coding:utf-8 -*-
import sys
import os
from bs4 import BeautifulSoup
import requests
import io
# 改变标准输出的默认编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
# 爬取微信读书书架书籍字数
shelf_arr = [
    "https://weread.qq.com/web/bookDetail/6613268071ae9e6f661ddee",
    "https://weread.qq.com/web/bookDetail/43f327705a48fc43feb9160",
    "https://weread.qq.com/web/bookDetail/e77320c071593303e779e6c",
]  # 分组所有书籍
# 将要输出保存的文件地址(自行更改),若文件不存在,则会自动创建
fw = open("./文本.txt", 'w', encoding='utf-8')
for link in shelf_arr:
    req = requests.get(url=link)
    req.encoding = "utf-8"
    html = req.text
    soup = BeautifulSoup(req.text, features="html.parser")
    book_titles = soup.find_all(
        "h2", class_="bookInfo_right_header_title_text")  # 书名
    book_authors = soup.find_all(
        "a", class_="bookInfo_author")  # 作者
    book_nums = soup.find_all("div", "introDialog_content_pub_line")  # 字数
    for book_title in book_titles:
        book_title_handle = "书名: " + book_title.text.strip()
        fw.write(book_title_handle)
        fw.write("\n")
    for book_author in book_authors:
        book_author_handle = "作者: " + book_author.text.strip()
        fw.write(book_author_handle)
        fw.write("\n")
    for book_num in book_nums:
        book_num_handles = book_num.find_all("span")
        for book_num_handle in book_num_handles:
            book_num_handle = book_num_handle.text.strip()
            fw.write(book_num_handle)
            fw.write("\n")
    fw.write("\n")

在这里插入图片描述

参考链接:https://blog.csdn.net/qq_41971835/article/details/115324144
参考链接:https://zhuanlan.zhihu.com/p/90157066

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值