修改项目12存在获取书单失败的问题

This commit is contained in:
sqzhang 2022-03-16 22:58:09 +08:00
parent 89eb6c169b
commit aaf4d4ea8c
7 changed files with 33 additions and 25 deletions

View File

@ -15,8 +15,6 @@
![](demo1.png)
<br />
![](demo2.png)
<br />
![](demo3.png)
<br />
<br />

Binary file not shown.

Before

Width:  |  Height:  |  Size: 135 KiB

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

View File

@ -5,7 +5,7 @@
@project: PyCharm
@file: pyqt_gui.py
@author: Shengqiang Zhang
@time: 2020/4/11 21:14
@time: 2022-03-16 21:35:46
@mail: sqzhang77@gmail.com
"""
@ -14,6 +14,7 @@ from excel_func import *
import sys
import os
import time
from tqdm import tqdm
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
@ -146,30 +147,40 @@ if __name__=='__main__':
books = get_bookshelf(USER_VID, HEADERS) # 获取书架上的书籍
books_finish_read = books['finishReadBooks']
books_finish_read = [[book.bookId, book.title, book.author, book.cover, book.intro, book.category] for book in books_finish_read]
books_finish_read = [[book.bookId, book.title, book.author, book.cover] for book in books_finish_read]
books_recent_read = books['recentBooks']
books_recent_read = [[book.bookId, book.title, book.author, book.cover, book.intro, book.category] for book in books_recent_read]
books_recent_read = [[book.bookId, book.title, book.author, book.cover] for book in books_recent_read]
books_all = books['allBooks']
books_all = [[book.bookId, book.title, book.author, book.cover, book.intro, book.category] for book in books_all]
write_excel_xls(data_dir + '我的书架.xls', ['已读完的书籍', '最近阅读的书籍', '所有的书籍'], [["ID", "标题", "作者", "封面", "简介", "所属目录"], ]) # 写入excel文件
books_all = [[book.bookId, book.title, book.author, book.cover] for book in books_all]
write_excel_xls(data_dir + '我的书架.xls', ['已读完的书籍', '最近阅读的书籍', '所有的书籍'], [["ID", "标题", "作者", "封面"], ]) # 写入excel文件
write_excel_xls_append(data_dir + '我的书架.xls', '已读完的书籍', books_finish_read) # 追加写入excel文件
write_excel_xls_append(data_dir + '我的书架.xls', '最近阅读的书籍', books_recent_read) # 追加写入excel文件
write_excel_xls_append(data_dir + '我的书架.xls', '所有的书籍', books_all) # 追加写入excel文件
# 获取书架上的每本书籍的笔记
for index, book in enumerate(books_finish_read):
# 获取【已读完的书籍】的笔记,如果想获取所有书籍的笔记,
# 请自行更改books_finish_read为books_all
pbar = tqdm(books_finish_read)
for book in pbar:
book_id = book[0]
book_name = book[1]
notes = get_bookmarklist(book[0], HEADERS)
with open(note_dir + book_name + '.txt', 'w', encoding='utf-8') as f:
f.write(notes)
print('导出笔记 {} ({}/{})'.format(note_dir + book_name + '.txt', index+1, len(books_finish_read)))
# 失败重试最大重试次数为4
for try_count in range(4):
try:
pbar.set_description("正在导出笔记【{}".format(book_name))
notes = get_bookmarklist(book[0], HEADERS)
with open(note_dir + book_name + '.txt', 'w', encoding='utf-8') as f:
f.write(notes)
# 写入成功后跳出循环,防止重复写入
break
except:
# 忽略异常,直接重试
pbar.set_description("获取笔记【{}】失败,开始第{}次重试".format(book_name, try_count + 1))
# 等待3秒后再重试
time.sleep(3)

View File

@ -3,5 +3,6 @@ clipboard
xlutils
xlrd
xlwt
tqdm
PyQt5==5.13.0
PyQtWebEngine==5.13.0

View File

@ -5,7 +5,7 @@
@project: PyCharm
@file: wereader.py
@author: Shengqiang Zhang
@time: 2020/4/11 21:14
@time: 2022-03-16 22:30:52
@mail: sqzhang77@gmail.com
"""
@ -27,7 +27,7 @@ import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# 书籍信息
Book = namedtuple('Book', ['bookId', 'title', 'author', 'cover', 'intro', 'category'])
Book = namedtuple('Book', ['bookId', 'title', 'author', 'cover'])
@ -149,11 +149,10 @@ def get_bookshelf(userVid, headers):
books_recent_read = set() # 最近阅读的书籍
books_all = set() # 书架上的所有书籍
for book in data['finishReadBooks']:
if not book['bookId'].isdigit(): # 过滤公众号
if ('bookId' not in book.keys()) or (not book['bookId'].isdigit()): # 过滤公众号
continue
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
b = Book(book['bookId'], book['title'], book['author'], book['cover'])
books_finish_read.add(b)
books_finish_read = list(books_finish_read)
books_finish_read.sort(key=itemgetter(-1)) # operator.itemgetter(-1)指的是获取对象的最后一个域的值即以category进行排序
@ -161,9 +160,9 @@ def get_bookshelf(userVid, headers):
for book in data['recentBooks']:
if not book['bookId'].isdigit(): # 过滤公众号
if ('bookId' not in book.keys()) or (not book['bookId'].isdigit()): # 过滤公众号
continue
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
b = Book(book['bookId'], book['title'], book['author'], book['cover'])
books_recent_read.add(b)
books_recent_read = list(books_recent_read)
books_recent_read.sort(key=itemgetter(-1)) # operator.itemgetter(-1)指的是获取对象的最后一个域的值即以category进行排序
@ -186,7 +185,7 @@ def get_notebooklist(headers):
books = []
for b in data['books']:
book = b['book']
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
b = Book(book['bookId'], book['title'], book['author'], book['cover'])
books.append(b)
books.sort(key=itemgetter(-1))
return books

View File

@ -528,7 +528,6 @@ python app.py
![](12.一键导出微信读书的书籍和笔记/demo1.png)
![](12.一键导出微信读书的书籍和笔记/demo2.png)
![](12.一键导出微信读书的书籍和笔记/demo3.png)
### 如何运行