全民阅读的时代已经来临,目前使用读书软件的用户数2.1亿,日活跃用户超过500万,其中19-35岁年轻用户占比超过60%,本科及以上学历用户占比高达80%,北上广深及其他省会城市/直辖市用户占比超过80%。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都网站制作、台安网络推广、微信小程序、台安网络营销、台安企业策划、台安品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供台安建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
本人习惯使用微信读书,为了方便整理书籍和导出笔记,便开发了这个小工具。
部分截图
代码思路
1. 目录结构
首先,我们先看一下整体目录结构
- ├─ excel_func.py 读写excel文件
 - ├─ pyqt_gui.py PyQt GUI界面
 - └─ wereader.py 微信读书相关api
 
使用xlrd和xlwt库对excel文件进行读写操作
使用PyQt绘制GUI界面
通过抓包解析获得相关api
2. excel_func.py
- def write_excel_xls(path, sheet_name_list, value):
 - # 新建一个工作簿
 - workbook = xlwt.Workbook()
 - # 获取需要写入数据的行数
 - index = len(value)
 - for sheet_name in sheet_name_list:
 - # 在工作簿中新建一个表格
 - sheet = workbook.add_sheet(sheet_name)
 - # 往这个工作簿的表格中写入数据
 - for i in range(0, index):
 - for j in range(0, len(value[i])):
 - sheet.write(i, j, value[i][j])
 - # 保存工作簿
 - workbook.save(path)
 
该函数的代码流程为:
3. pyqt_gui.py
- class MainWindow(QMainWindow):
 - def __init__(self, *args, **kwargs):
 - super().__init__(*args, **kwargs)
 - self.DomainCookies = {}
 - self.setWindowTitle('微信读书助手') # 设置窗口标题
 - self.resize(900, 600) # 设置窗口大小
 - self.setWindowFlags(Qt.WindowMinimizeButtonHint) # 禁止最大化按钮
 - self.setFixedSize(self.width(), self.height()) # 禁止调整窗口大小
 - url = 'https://weread.qq.com/#login' # 目标地址
 - self.browser = QWebEngineView() # 实例化浏览器对象
 - QWebEngineProfile.defaultProfile().cookieStore().deleteAllCookies() # 初次运行软件时删除所有cookies
 - QWebEngineProfile.defaultProfile().cookieStore().cookieAdded.connect(self.onCookieAdd) # cookies增加时触发self.onCookieAdd()函数
 - self.browser.loadFinished.connect(self.onLoadFinished) # 网页加载完毕时触发self.onLoadFinished()函数
 - self.browser.load(QUrl(url)) # 加载网页
 - self.setCentralWidget(self.browser) # 设置中心窗口
 
该函数的代码流程为:
- # 网页加载完毕事件
 - def onLoadFinished(self):
 - global USER_VID
 - global HEADERS
 - # 获取cookies
 - cookies = ['{}={};'.format(key, value) for key,value in self.DomainCookies.items()]
 - cookies = ' '.join(cookies)
 - # 添加Cookie到header
 - HEADERS.update(Cookie=cookies)
 - # 判断是否成功登录微信读书
 - if login_success(HEADERS):
 - print('登录微信读书成功!')
 - # 获取用户user_vid
 - if 'wr_vid' in self.DomainCookies.keys():
 - USER_VID = self.DomainCookies['wr_vid']
 - print('用户id:{}'.format(USER_VID))
 - # 关闭整个qt窗口
 - self.close()
 - else:
 - print('请扫描二维码登录微信读书...')
 
该函数的代码流程为:
- # 添加cookies事件
 - def onCookieAdd(self, cookie):
 - if 'weread.qq.com' in cookie.domain():
 - name = cookie.name().data().decode('utf-8')
 - value = cookie.value().data().decode('utf-8')
 - if name not in self.DomainCookies:
 - self.DomainCookies.update({name: value})
 
该函数的代码流程为:
- books = get_bookshelf(USER_VID, HEADERS) # 获取书架上的书籍
 - booksbooks_finish_read = books['finishReadBooks']
 - booksbooks_recent_read = books['recentBooks']
 - booksbooks_all = books['allBooks']
 - 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):
 - bookbook_id = book[0]
 - bookbook_name = book[1]
 - notes = get_bookmarklist(book[0], HEADERS)
 - with open(note_dir + book_name + '.txt', 'w') as f:
 - f.write(notes)
 - print('导出笔记 {} ({}/{})'.format(note_dir + book_name + '.txt', index+1, len(books_finish_read)))
 
该函数的代码流程为:
4. wereader.py
- def get_bookshelf(userVid, headers):
 - """获取书架上所有书"""
 - url = "https://i.weread.qq.com/shelf/friendCommon"
 - params = dict(userViduserVid=userVid)
 - r = requests.get(url, paramsparams=params, headersheaders=headers, verify=False)
 - if r.ok:
 - data = r.json()
 - else:
 - raise Exception(r.text)
 - books_finish_read = set() # 已读完的书籍
 - books_recent_read = set() # 最近阅读的书籍
 - books_all = set() # 书架上的所有书籍
 - for book in data['recentBooks']:
 - if not book['bookId'].isdigit(): # 过滤公众号
 - continue
 - b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
 - books_recent_read.add(b)
 - books_all = books_finish_read + books_recent_read
 - return dict(finishReadBooks=books_finish_read, recentBooks=books_recent_read, allBooks=books_all)
 
该函数的代码流程为:
- def get_bookmarklist(bookId, headers):
 - """获取某本书的笔记返回md文本"""
 - url = "https://i.weread.qq.com/book/bookmarklist"
 - params = dict(bookIdbookId=bookId)
 - r = requests.get(url, paramsparams=params, headersheaders=headers, verify=False)
 - if r.ok:
 - data = r.json()
 - # clipboard.copy(json.dumps(data, indent=4, sort_keys=True))
 - else:
 - raise Exception(r.text)
 - chapters = {c['chapterUid']: c['title'] for c in data['chapters']}
 - contents = defaultdict(list)
 - for item in sorted(data['updated'], key=lambda x: x['chapterUid']):
 - # for item in data['updated']:
 - chapter = item['chapterUid']
 - text = item['markText']
 - create_time = item["createTime"]
 - start = int(item['range'].split('-')[0])
 - contents[chapter].append((start, text))
 - chapters_map = {title: level for level, title in get_chapters(int(bookId), headers)}
 - res = ''
 - for c in sorted(chapters.keys()):
 - title = chapters[c]
 - res += '#' * chapters_map[title] + ' ' + title + '\n'
 - for start, text in sorted(contents[c], key=lambda e: e[0]):
 - res += '> ' + text.strip() + '\n\n'
 - res += '\n'
 - return res
 
该函数的代码流程为:
如何运行
- # 跳转到当前目录
 - cd 目录名
 - # 先卸载依赖库
 - pip uninstall -y -r requirement.txt
 - # 再重新安装依赖库
 - pip install -r requirement.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
 - # 开始运行
 - python pyqt_gui.py
 
补充
完整版源代码存放在github上,有需要的请点击这里下载
https://github.com/shengqiangzhang/examples-of-web-crawlers
                当前名称:Python 一键导出微信阅读记录和笔记
                
                URL分享:http://www.csdahua.cn/qtweb/news48/80798.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网