本文目录导读:

我来介绍几种批量URL编码/解码的方法:
使用Python脚本
批量URL编码
#!/usr/bin/env python3
import urllib.parse
import sys
def batch_url_encode(file_path):
"""批量URL编码"""
with open(file_path, 'r', encoding='utf-8') as f:
urls = f.readlines()
for url in urls:
url = url.strip()
if url:
encoded = urllib.parse.quote(url, safe='')
print(encoded)
def batch_url_decode(file_path):
"""批量URL解码"""
with open(file_path, 'r', encoding='utf-8') as f:
urls = f.readlines()
for url in urls:
url = url.strip()
if url:
decoded = urllib.parse.unquote(url)
print(decoded)
if __name__ == "__main__":
# 使用示例
input_file = "urls.txt" # 每行一个URL
print("URL编码结果:")
batch_url_encode(input_file)
print("\nURL解码结果:")
batch_url_decode(input_file)
带参数的高级版本
#!/usr/bin/env python3
import urllib.parse
import argparse
import sys
def process_urls(input_file, output_file, mode, safe_chars=''):
"""处理URL编码/解码"""
try:
with open(input_file, 'r', encoding='utf-8') as f:
urls = f.readlines()
except FileNotFoundError:
print(f"错误:找不到文件 {input_file}")
sys.exit(1)
results = []
for url in urls:
url = url.strip()
if url:
if mode == 'encode':
result = urllib.parse.quote(url, safe=safe_chars)
else: # decode
result = urllib.parse.unquote(url)
results.append(result)
if output_file:
with open(output_file, 'w', encoding='utf-8') as f:
for result in results:
f.write(result + '\n')
print(f"结果已保存到 {output_file}")
else:
for result in results:
print(result)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='批量URL编码/解码工具')
parser.add_argument('-i', '--input', required=True, help='输入文件路径')
parser.add_argument('-o', '--output', help='输出文件路径(可选)')
parser.add_argument('-m', '--mode', choices=['encode', 'decode'],
default='encode', help='编码或解码')
parser.add_argument('-s', '--safe', default='/',
help='编码时保留的字符(默认保留斜杠)')
args = parser.parse_args()
process_urls(args.input, args.output, args.mode, args.safe)
使用bash脚本
批量URL编码
#!/bin/bash
# 批量URL编码脚本
# 使用方法:./url_encode.sh urls.txt
input_file="$1"
if [ -z "$input_file" ]; then
echo "用法:$0 <输入文件>"
echo "文件每行包含一个URL"
exit 1
fi
while IFS= read -r url; do
if [ -n "$url" ]; then
encoded_url=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$url', safe=''))")
echo "$encoded_url"
fi
done < "$input_file"
批量URL解码
#!/bin/bash
# 批量URL解码脚本
# 使用方法:./url_decode.sh urls.txt
input_file="$1"
if [ -z "$input_file" ]; then
echo "用法:$0 <输入文件>"
echo "文件每行包含一个URL"
exit 1
fi
while IFS= read -r url; do
if [ -n "$url" ]; then
decoded_url=$(python3 -c "import urllib.parse; print(urllib.parse.unquote('$url'))")
echo "$decoded_url"
fi
done < "$input_file"
使用Node.js
#!/usr/bin/env node
// batch_url.js
const fs = require('fs');
const readline = require('readline');
function processURLs(inputFile, mode, outputFile) {
const rl = readline.createInterface({
input: fs.createReadStream(inputFile),
crlfDelay: Infinity
});
const output = [];
rl.on('line', (line) => {
if (line.trim()) {
let result;
if (mode === 'encode') {
result = encodeURIComponent(line);
} else {
result = decodeURIComponent(line);
}
output.push(result);
}
});
rl.on('close', () => {
if (outputFile) {
fs.writeFileSync(outputFile, output.join('\n'), 'utf8');
console.log(`结果已保存到 ${outputFile}`);
} else {
output.forEach(line => console.log(line));
}
});
}
// 使用示例
const inputFile = 'urls.txt';
processURLs(inputFile, 'encode', 'encoded_urls.txt');
processURLs(inputFile, 'decode', 'decoded_urls.txt');
使用在线工具(简单场景)
对于少量数据,也可以使用在线工具:
- https://www.urlencoder.org/
- https://meyerweb.com/eric/tools/dencoder/
- 或搜索引擎搜索"URL编码解码工具"
使用方法
-
准备输入文件:创建
urls.txt,每行一个URL -
运行脚本:
# Python版本 python3 batch_url.py -i urls.txt -m encode -o encoded.txt python3 batch_url.py -i urls.txt -m decode -o decoded.txt # Bash版本 chmod +x url_encode.sh ./url_encode.sh urls.txt > encoded.txt
注意事项
- 编码时
safe=''表示编码所有特殊字符,保留空字符串表示全部编码 - 编码时
safe='/:?=&'可以保留URL结构字符 - 中文等非ASCII字符通常需要编码
- 解码时要注意误解码,避免破坏URL结构
选择适合你环境的方法即可!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播