Skip to main content
 首页 » 编程设计

尝试使用日语编码调用 wget 时 Python 表现得很奇怪

2024年10月01日7lidabo

我正在创建一个 python 脚本,该脚本使用 bash 对文件中包含的日语单词列表执行 wget。我只会使用 curl 但这有编码问题。使用 wget 它确实下载了 html,但它以诗意的标题将其转储到当前目录中,例如:

   試%E8%A1%8C%E9%8C%AF誤 

我想让它把 html 放到像“output/混合.txt”这样漂亮的地方。它确实创建了这些听起来很漂亮的文件,但其中没有任何内容。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
 
import os 
 
with open("words") as f: 
    for line in f: 
        text = unicode(line, "utf-8") 
        os.system("wget \'https://kotobank.jp/word/" + line.strip() + "'> output/" + line.strip() + ".txt") 
        #print("wget \'https://kotobank.jp/word/" + line.strip() + "'>> output/out.txt") 

而文件“words”如下:

追究 
花器 
陶磁器 
枯渇 
風合い 
繊維 
混合 
アボード 
受け継い 
試行錯誤 
硬質 

请您参考如下方法:

使用 -O file 选项而不是重定向输出:

os.system("wget \'https://kotobank.jp/word/" + line.strip() + "' -O " + line.strip() + ".txt" 

参见wget documentation获取更多信息。