当前位置: 首页 > news >正文

爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理

背景+适用情况介绍

老的荣耀手机属于华为云系统,家里人换了新荣耀手机属于荣耀云系统无法通过云空间将备忘录转移到新手机,不想让他们一个一个搞,于是整了一晚上想办法爬取下来。从网页抓取下来,然后存到docx文档中(包括文字和图片,别的形式的内容请举一反三) 多行图片多行文字sample:
演示gif
本方法Cons:不能复制到荣耀云里,因为捣了半天这荣耀云根本就没有除了手机之外可以访问的方法
续:别人告诉我有荣耀云win客户端,本来还想看看导入,太抽象了,竟然说在这里插入图片描述
,辣鸡

别的思路

手机内部自动化保存为文档后处理
华为手机备忘录批量导出txt╳ 全自动版 ╳By 免ROOT自动化助手
cons:这个up是导出一条删除一条,我不想删除,它有for循环但是要付费,付费倒不是啥别太贵就行毕竟人家做的工具,但是一看还要注册登录,,,

fiddler抓包方式抓所有笔记页面
华为手机备忘录批量导出文字和图片
cons:看了一下需要手动一个一个点笔记,不想动了

前置准备

  • 首先确定老手机在设置的主账号登录进去或者注册是华为云,让家里注册了一下,然后打开同步把数据同步到云端,登录华为云空间可以看到所有的备忘录,点进去记录第一个备忘录的URL后面都要用
  • 需要chrome(查看版本:右上角三个点-help-about)和版本匹配的chromedriver(别的可以控制浏览器控件如firefox也可以,我这样用)
    chromedriver.exe的目录需要加入环境变量path,cmd运行 chromedriver 可测试,如果调用里自定义位置可以尝试path参数,库接口已更新与文档不匹配
  • python环境

代码步骤

in simulate

  • 第一次扫码登录,登录完毕python控制台任意enter继续,自动保存cookies到cookies.json后续不用登录

  • 手动浏览器f12分析元素可知中间一列是每页链接列表,右边一列是点击后的笔记标题时间内容,找到dom树下到达这个元素的路径,可覆盖到元素然后右键复制path-xpath得到,示例如下:
    中间目录内容

  • 对每个元素点击,滑动到下一个(必须滑动,为了点击下一个做准备,否则直接代码点击超出视窗的元素无法加载右侧笔记内容)

  • 相关知识链接
    – selenium-python document
    – xPath examples 注意dom css中序数是从1开始
    – 滑动界面外的元素问题
    – 获取元素标签内容和属性值例子

  • 根据xpath提取标题,时间,内容,内容由分析可知是一个一个div行叠加的,图文都是在行中,所以内循环提取一行一行内容

  • 内循环-图:request无法下载?控制浏览器下载,从默认目录移动到当前/img下并加上jpg后缀(因为看了一下上传只能传jpg,下载时候看了一下都可以用jpg打开)图文件名保存在列表中备用(应该没有重复的)

  • 重新组成数据格式如下,or whatever you want,然后存到result.json
    – [title0, time0, [content0 line0 text, content0 line1 img name, …]]
    – [title1, …]

in result_to_doc

  • 很简单一行一行写入
  • 相关链接
    – python-docx quick start 注意docx库实际要装的是python-docx,如果import docx有错看装的对不对

project结构以及代码

在这里插入图片描述

simulate.py

import json
import os
import shutil
import timeimport requests as requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import Bydef login_and_save_to_cookies(driver, url):# driver = webdriver.Chrome()driver.get(url)val = input("Manually login now. Enter anything if finished")# time.sleep(20)  # Let the user actually see something!cookies = driver.get_cookies()# 转换成字符串保存json_cookie = json.dumps(cookies)# 保存到txt文件with open('cookies.json', 'w') as f:f.write(json_cookie)print('cookies保存成功!')def login_with_cookies(driver):with open('cookies.json','r',encoding='utf8') as f:cookies = json.loads(f.read())# 给浏览器添加cookiesfor cookie in cookies:driver.add_cookie(cookie)# 刷新网页,cookies才会成功,后面我们直接跳转就不用刷新了# driver.refresh()if __name__ == '__main__':driver = webdriver.Chrome()# 笔记的第一页url,后面需要用到url = 'https://cloud.huawei.com/home#/notepad/note/allNote/note/xxxxxxxxxxxxxxxxxxxxxxx'# 加载已有cookies之前至少得先访问一遍driver.get(url)time.sleep(5)  # 需要等待加载完# 切换成中文language_button = driver.find_element(By.XPATH,'//*[@id="Cloudlogin"]/div[1]/div[1]/div[2]/a[1]/span[1]')ActionChains(driver).click(language_button).perform()  # single clickchinese_button = driver.find_element(By.XPATH,'//*[@id="scroller"]/ul/li[65]/div')ActionChains(driver).click(chinese_button).perform()  # single clicktime.sleep(5)val = input("If using existing cookies, type 'n', manually login type anything else")if val != 'n':login_and_save_to_cookies(driver, url)login_with_cookies(driver)time.sleep(5)driver.get(url)time.sleep(5)# driver.find_element(By.)note_center_items = driver.find_elements(By.CLASS_NAME, "note_item")note_titles = []note_times = []note_contents = []chrome_download_directory = "C:\\Users\\xxxxxxxxxxxxxx\\Downloads\\"  # 浏览器默认下载路径img_store_directory = "./img/"  # 都需要以/结尾以便后面拼接for note_center_item in note_center_items:# print(note_center_item.get_attribute("autokey"))ac = note_center_item# ActionChains(driver).scroll_to_element(ac).perform()ActionChains(driver).click(ac).perform()  # single clickdriver.execute_script("arguments[0].scrollIntoView(true);", ac)  # 必须滑动,不然点击视窗外的元素无法加载右侧笔记内容time.sleep(5)title_element = driver.find_element(By.XPATH,'//*[@id="note_title_editor"]/div/div[6]/div[1]/div/div/div/div[5]/pre/span')time_element = driver.find_element(By.XPATH,'//*[@id="app"]/div[6]/div[3]/div/div[1]/div[4]/div[2]/div/span[1]')content_multiline_container_element = driver.find_element(By.XPATH,'//*[@id="note_detail_editor"]/div/div[6]/div[1]/div/div/div/div[5]')content_singleline_containers = content_multiline_container_element.find_elements(By.XPATH, './div')note_content = []for i in range(len(content_singleline_containers)):content_singleline_container = content_singleline_containers[i]try:# is an imageimg_element = content_singleline_container.find_element(By.XPATH, './div/div/img')img_src = img_element.get_attribute("src")# 用浏览器下载,这里request好像不行driver.get(img_src)# cur_img = requests.get(img_src)# note_content.append(cur_img)# 等待下载完time.sleep(3)# 转移,加jpg后缀因为这里都是jpg格式downloaded_file_name = img_src.split("/")[-1]shutil.move(chrome_download_directory + downloaded_file_name, img_store_directory)os.rename(img_store_directory + downloaded_file_name, img_store_directory + downloaded_file_name + '.jpg')note_content.append(downloaded_file_name + '.jpg')  # 标记位置except:# is textnote_content.append(content_singleline_container.text)  # 可以直接获取最里面的值, 也可以通过./pre/span/span获取最里面的元素再获取text# 如果全是文字可以直接合并成段落# if i < len(content_singleline_containers) - 1:#     note_content += "\n"note_title = title_element.textnote_time = time_element.textnote_titles.append(note_title)note_times.append(note_time)note_contents.append(note_content)print(note_title)print(note_time)print(note_content)print(len(note_titles), len(note_times), len(note_contents))result = []for i in range(len(note_titles)):result.append([note_titles[i], note_times[i], note_contents[i]])with open('result.json', 'w') as f:json.dump(result, f)# f = open("result.txt", "w")# for i in range(len(note_times)):#     f.write(note_times[i])#     f.write(note_contents[i])#     f.write("\n")# f.close()# time.sleep(20)

result_to_doc.py

import jsonfrom docx import Document
from docx.shared import Inchesdocument = Document()with open('result.json', 'r') as f:data_json = json.load(f)img_store_directory = "./img/"
for note_title, note_time, note_content in data_json:if len(note_title) > 0:# 这里很多都是空标题所以如果空就不写入document.add_paragraph(note_title)document.add_paragraph(note_time)for note_content_line in note_content:if note_content_line.split('.')[-1] == 'jpg':# is an imagedocument.add_picture(img_store_directory + note_content_line, width=Inches(5))else:document.add_paragraph(note_content_line)document.add_paragraph("\n")document.save('备忘录.docx')
http://www.lryc.cn/news/301380.html

相关文章:

  • 网络安全的新防线:主动进攻,预防为先
  • 基于java springboot+mybatis学生学科竞赛管理管理系统设计和实现
  • 秒懂百科,C++如此简单丨第二十一天:栈和队列
  • STM32-开发环境之STM32CubeMX
  • [晓理紫]CCF系列会议截稿时间订阅
  • 重复导航到当前位置引起的。Vue Router 提供了一种机制,阻止重复导航到相同的路由路径。
  • 如何在 Angular 中使用 Flex 布局
  • 通俗的讲解什么是机器学习之损失函数
  • 快速搭建PyTorch环境:Miniconda一步到位
  • 图灵日记之java奇妙历险记--抽象类和接口
  • 批量给元素添加进场动画;获取文本光标位置;项目国际化
  • 解决:docker创建Redis容器成功,但无法启动Redis容器、也无报错提示
  • Jlink+OpenOCD+STM32 Vscode 下载和调试环境搭建
  • 单片机在物联网中的应用
  • 16.Qt 工具栏生成
  • 【Linux内核】从0开始入门Linux Kernel源码
  • SQL Service 2008 的安装与配置
  • Apache POI | Java操作Excel文件
  • vue 学习definproperty方法
  • react 实现路由拦截
  • 数据分析(一) 理解数据
  • 什么是 Flet?
  • 多模态(三)--- BLIP原理与源码解读
  • 掌握高性能SQL的34个秘诀多维度优化与全方位指南
  • 美国纳斯达克大屏怎么投放:投放完成需要多长时间-大舍传媒Dashe Media
  • 【MySQL】多表关系的基本学习
  • Springboot之接入gRPC
  • 2023年中国数据智能管理峰会(DAMS上海站2023):核心内容与学习收获(附大会核心PPT下载)
  • DS:八大排序之堆排序、冒泡排序、快速排序
  • Sora:继ChatGPT之后,OpenAI的又一力作