修改项目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) ![](demo1.png)
<br /> <br />
![](demo2.png) ![](demo2.png)
<br />
![](demo3.png)
<br /> <br />
<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 @project: PyCharm
@file: pyqt_gui.py @file: pyqt_gui.py
@author: Shengqiang Zhang @author: Shengqiang Zhang
@time: 2020/4/11 21:14 @time: 2022-03-16 21:35:46
@mail: sqzhang77@gmail.com @mail: sqzhang77@gmail.com
""" """
@ -14,6 +14,7 @@ from excel_func import *
import sys import sys
import os import os
import time import time
from tqdm import tqdm
from PyQt5.QtWidgets import QMainWindow from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
@ -146,30 +147,40 @@ if __name__=='__main__':
books = get_bookshelf(USER_VID, HEADERS) # 获取书架上的书籍 books = get_bookshelf(USER_VID, HEADERS) # 获取书架上的书籍
books_finish_read = books['finishReadBooks'] 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 = 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 = books['allBooks']
books_all = [[book.bookId, book.title, book.author, book.cover, book.intro, book.category] for book in books_all] 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(data_dir + '我的书架.xls', ['已读完的书籍', '最近阅读的书籍', '所有的书籍'], [["ID", "标题", "作者", "封面"], ]) # 写入excel文件
write_excel_xls_append(data_dir + '我的书架.xls', '已读完的书籍', books_finish_read) # 追加写入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_recent_read) # 追加写入excel文件
write_excel_xls_append(data_dir + '我的书架.xls', '所有的书籍', books_all) # 追加写入excel文件 write_excel_xls_append(data_dir + '我的书架.xls', '所有的书籍', books_all) # 追加写入excel文件
# 获取【已读完的书籍】的笔记,如果想获取所有书籍的笔记,
# 获取书架上的每本书籍的笔记 # 请自行更改books_finish_read为books_all
for index, book in enumerate(books_finish_read): pbar = tqdm(books_finish_read)
for book in pbar:
book_id = book[0] book_id = book[0]
book_name = book[1] book_name = book[1]
notes = get_bookmarklist(book[0], HEADERS)
with open(note_dir + book_name + '.txt', 'w', encoding='utf-8') as f: # 失败重试最大重试次数为4
f.write(notes) for try_count in range(4):
try:
pbar.set_description("正在导出笔记【{}".format(book_name))
print('导出笔记 {} ({}/{})'.format(note_dir + book_name + '.txt', index+1, len(books_finish_read))) 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 xlutils
xlrd xlrd
xlwt xlwt
tqdm
PyQt5==5.13.0 PyQt5==5.13.0
PyQtWebEngine==5.13.0 PyQtWebEngine==5.13.0

View File

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

View File

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