You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Shodan Parse处理json.gz文件时遇charmap编码错误的解决方法

解决Shodan Parse命令的UnicodeEncodeError问题

你遇到的这个UnicodeEncodeError是因为Windows命令提示符默认使用CP1252编码,而Shodan导出的数据里包含了该编码无法处理的Unicode字符,导致输出重定向时编码失败。下面是几种可行的解决方法:

方法1:临时切换命令行编码为UTF-8

在执行Shodan命令之前,先运行以下命令切换命令行的编码为UTF-8:

chcp 65001

然后再执行你的Shodan解析命令:

shodan parse --fields port,isp,hostnames,location,timestamp,org,domain,transport,vulns,ssl,http.server,ip_str --separator , shodan-export.json.gz > shodan-export-proc.json.gz

注意:切换编码后命令行字体可能出现显示异常,但不会影响输出文件的正确性。

方法2:使用Python脚本手动处理(更可靠)

如果方法1仍有问题,可以写一个简单的Python脚本替代shodan parse命令,手动指定UTF-8编码输出:

import gzip
import json

# 读取压缩的Shodan导出文件
with gzip.open('shodan-export.json.gz', 'rt', encoding='utf-8') as infile:
    # 打开输出文件,指定UTF-8编码
    with gzip.open('shodan-export-proc.json.gz', 'wt', encoding='utf-8') as outfile:
        for line in infile:
            try:
                data = json.loads(line)
                # 提取需要的字段
                extracted = {
                    'port': data.get('port'),
                    'isp': data.get('isp'),
                    'hostnames': data.get('hostnames'),
                    'location': data.get('location'),
                    'timestamp': data.get('timestamp'),
                    'org': data.get('org'),
                    'domain': data.get('domain'),
                    'transport': data.get('transport'),
                    'vulns': data.get('vulns'),
                    'ssl': data.get('ssl'),
                    'http.server': data.get('http', {}).get('server'),
                    'ip_str': data.get('ip_str')
                }
                # 写入JSON行
                json.dump(extracted, outfile, ensure_ascii=False)
                outfile.write('\n')
            except Exception as e:
                print(f"处理行时出错: {e}")

把这个脚本保存为parse_shodan.py,然后在Anaconda环境中运行:

python parse_shodan.py

这个方法直接控制了输入和输出的编码,完全规避了命令行编码的限制。

方法3:修改Shodan命令的输出编码(不推荐)

如果你不想写脚本,也可以修改Shodan的脚本文件来强制使用UTF-8输出。找到shodan-script.py所在路径(你的路径是D:\Software\Anaconda\Scripts\shodan-script.py),修改调用echo的部分,指定编码为UTF-8。不过这种方法需要修改第三方库代码,后续更新Shodan时可能会被覆盖,仅作为备选方案。

错误原因说明

Windows默认命令行编码(CP1252)仅支持有限字符集,而Shodan收集的数据包含全球各地的Unicode字符(比如非英语地名、组织名),这些字符无法用CP1252编码,因此重定向输出时会抛出编码错误。通过切换UTF-8编码或手动控制脚本编码,就能解决这个问题。

内容的提问来源于stack exchange,提问作者JsDart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:21:54