0


爬虫-华为云空间备忘录导出到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 time

import requests as requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

deflogin_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文件withopen('cookies.json','w')as f:
        f.write(json_cookie)print('cookies保存成功!')deflogin_with_cookies(driver):withopen('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 click
    chinese_button = driver.find_element(By.XPATH,'//*[@id="scroller"]/ul/li[65]/div')
    ActionChains(driver).click(chinese_button).perform()# single click
    time.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 click
        driver.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 inrange(len(content_singleline_containers)):
            content_singleline_container = content_singleline_containers[i]try:# is an image
                img_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 text
                note_content.append(content_singleline_container.text)# 可以直接获取最里面的值, 也可以通过./pre/span/span获取最里面的元素再获取text# 如果全是文字可以直接合并成段落# if i < len(content_singleline_containers) - 1:#     note_content += "\n"
        note_title = title_element.text
        note_time = time_element.text

        note_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 inrange(len(note_titles)):
        result.append([note_titles[i], note_times[i], note_contents[i]])withopen('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 json

from docx import Document
from docx.shared import Inches

document = Document()withopen('result.json','r')as f:
    data_json = json.load(f)

img_store_directory ="./img/"for note_title, note_time, note_content in data_json:iflen(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 image
            document.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')

本文转载自: https://blog.csdn.net/violet_ever_garden/article/details/136109008
版权归原作者 violet_ever_garden 所有, 如有侵权,请联系我们删除。

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

还没有评论