导出有道词典单词本

2023-08-06
#python #有道词典 #爬虫

前言

有道词典功能太多了,奈何我只想要一个查单词、背单词然后没有广告的软件,所以最后决定投入欧陆词典的怀抱,迁移到过程中突然发现竟然 Macos 和 IOS 的有道词典没有导出单词本的功能,只好自己动手了。

导出脚本

import browser_cookie3
import requests
import time
import random

cj = browser_cookie3.chrome()


def get_words_from_youdao(limit, offset,sort, lanTo, lanFrom):

    url = "https://dict.youdao.com/wordbook/webapi/v2/word/list"
    resp = requests.get(url=url,params={
        "limit": limit,
        "offset": offset,
        "sort": sort,
        "lanTo": lanTo,
        "lanFrom": lanFrom
    }, cookies=cj)
    if resp.status_code != 200:
        print("请求有道单词本失败,请确保已经登录有道单词本: https://www.youdao.com/webwordbook/wordlist")
        return [], 0
    
    response = resp.json()
    total = response["data"]["total"]
    words = response["data"]["itemList"]
    return [word["word"] for word in words], total

def export_words_to_txt(limit=48, offset=0, sort="time", lanTo=None, lanFrom=None):

    words, total = get_words_from_youdao(limit, offset, sort, lanTo, lanFrom)

    with open("words.txt", "a+") as f:
        f.write("\n".join(words))
        f.write("\n")
    
    offset += limit
    print("当前导出进度: {}/{}".format(total if offset > total else offset, total))
    
    if offset  > total:
        print("导出完成")
        return
    
    sleep_time = random.randint(1, 3)
    print("休息{}秒".format(sleep_time))
    time.sleep(sleep_time)

    return export_words_to_txt(limit, offset, sort, lanTo, lanFrom)
        
    

if __name__ == "__main__":
    export_words_to_txt()

运行截图

image

最终导出一个 txt 文件,然后在欧路词典的网页端 https://my.eudic.net/studylist/import/ 进行导入

image