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

这篇具有很好参考价值的文章主要介绍了爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

背景+适用情况介绍

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

别的思路

手机内部自动化保存为文档后处理
华为手机备忘录批量导出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得到,示例如下:
    爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理,爬虫,华为云,selenium爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理,爬虫,华为云,selenium

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

  • 相关知识链接
    – 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结构以及代码

爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理,爬虫,华为云,selenium文章来源地址https://www.toymoban.com/news/detail-826855.html

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


def 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())
    # 给浏览器添加cookies
    for 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 in range(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 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 json

from docx import Document
from docx.shared import Inches

document = 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 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')

到了这里,关于爬虫-华为云空间备忘录导出到docx-selenium控制浏览器行为-python数据处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 苹果备忘录如何转移?备忘录怎么转移到新手机?

    对于很多苹果手机的用户而言,想必都有使用备忘录的习惯吧?但是,经过长期的使用,当需要更换手机设备时,您考虑过如何转移这些记录吗? 苹果备忘录怎么转移到新手机? 如您使用的是苹果手机内置的备忘录,并且需要更换的新设备依然是苹果手机,其解决方案非常

    2024年02月13日
    浏览(41)
  • 苹果手机备忘录如何导入新手机?手机备忘录怎么转移?

    一般来说,大多数手机用户更换手机的频率是3—5年,在一部手机使用了几年之后,就会出现内存不足、系统卡顿、电池续航时间较短等问题,这时候就需要更换新的手机了。有不少苹果手机用户在更换新手机的时候,都很发愁一个问题,这就是手机备忘录如何导入新手机。

    2024年01月25日
    浏览(33)
  • 备忘录模式

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。 1.1 撤销操作: 例如,在文本编辑器中,当我们对文本进行修改时,可以使用备忘录模式来实现撤销操作。每次对文本进行修改时,就保存当

    2024年02月03日
    浏览(31)
  • 备忘录模式(Memento)

    备忘录模式是一种行为设计模式,在不破坏封装性的前提下,允许在不暴露对象实现细节的情况下保存和恢复对象之前的状态。 一个备忘录(memento)是一个对象,它存储另一个对象在某个瞬间的内部状态,而后者称为备忘录的原发器(originator)。当需要设置原发器的检查点时,取

    2024年02月13日
    浏览(26)
  • 博文备忘录

    https://blog.csdn.net/ZQ_KING/article/details/80320080 (链接层)MAC地址——ARP协议 (网络层)IP地址——IP协议 (传输层)端口——UDP协议、TCP协议 (应用层)域名——HTTP协议、HTTPS协议 https://github.com/xiaomuzhu/front-end-interview/blob/master/docs/guide/http.md (http) https://github.com/xiaomuzhu/front-

    2024年02月08日
    浏览(29)
  • stata备忘录

    1. 画图 (1)时间趋势图 等价命令 字体大小 option 字体大小option description zero no size whatsoever, vanishingly small minuscule smallest quarter_tiny third_tiny half_tiny tiny vsmall small medsmall medium medlarge large vlarge huge vhuge largest tenth one-tenth the size of the graph quarter one-fourth the size of the graph third one-thi

    2024年02月03日
    浏览(28)
  • iPhone备忘录清除缓存

    离剁手只有一张图,就是: 遇到问题就去找方法,找方法就要找问题源头,经过我不懈的努力,我终于找到了万恶之源——备忘录。 奇怪的是我的东西并不多,而且写图文总结的备忘录,一般都会放在iCloud上,前几日我一直是觉得自己的图片放得太多才导致如此,但狂删除

    2024年02月07日
    浏览(34)
  • 安卓adb调试备忘录

    由于 MAC 的 USB 口全被占用着,采用无线连接刚方便,记录一下,以防忘记~ USB 调试 手机开启开发者选项和 USB 调试模式(通常开启方式:在关于手机连续点击7次版本号); USB 连接线和各种接口正常(个别三方数据线仅支持充电,不支持调试); 驱动状态正常(Linux 和 Mac

    2024年02月21日
    浏览(34)
  • 【设计模式】备忘录模式

    主页传送门: 💁 传送        备忘录(Memento Pattern)模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式。。其定义如下: Without violating encapsulation,capture and externalize an object’s internal state so that the object can be restored to this state later.        即:在不破坏封

    2024年02月07日
    浏览(38)
  • C语言备忘录一

    int arr[5] = {1, 2, 3, 4, 5}😭 数组 【】中的数字大小是从一开始数,{}中的大小是从零开始数) extern extern表明变量或者函数是定义在其他其他文件中的 int a = 5与int a = 5意义是一样的,都是定义。而extern int a;是声明。但会产生一条警告; extern \\\"C\\\"的主要作用就是为了能够正确实现

    2024年02月06日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包