YIMI's Blog
笔记是一种心态!

GoldenDict开源词典配置搜狗翻译

  1. 编写python脚本,保存SogouTranslatorGD.py,放入指定目录下
#!/usr/bin/python3
import urllib.request
import sys
import chardet
from html import unescape
from urllib.parse import urljoin, quote
import io
import random

def get_page(url):
    # 生成7到16之间的随机整数
    ios_version = random.randint(11, 17)
    # 生成80到130之间的随机整数
    num2 = random.randint(118, 126)
    headers = {
        'User-Agent': f'Mozilla/5.0 (iPhone; CPU iPhone OS {ios_version} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) iOS/{num2}.0.6312.52 Mobile Safari/604.1',
        'Accept': '*/*'
    }
    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)
    html = response.read()
    response.close()
    return html
css = """<style type="text/css">
.programs_html .tab-trans-main .dict .module+.module {
    border-top-color: none !important;
    border-top: 3px dashed #6f6f6c;
}

.programs_html .tab-trans-main .dict .module h3 {
    color: rgb(229 149 33) !important;
    font-size: 18px !important;
    font-weight: bold !important;
}

.programs_html .header-wap-new,
.programs_html .select-area,
.programs_html .sort-select,
.programs_html .sgui-content,
.programs_html .trans-to .pronounce-box,
.programs_html .operate-btn-box,
.programs_html .footer-wap,
.programs_html .loading-pc,
.programs_html .input-area .btn-clear,
.programs_html #encyclopedia,
.programs_html .empty-space {
    display: none !important;
}

.programs_html .trans-box {
    cursor: not-allowed;
    pointer-events: none;
    box-shadow: #2A82DA 0px 16px 40px 0px !important;
}

.programs_html .trans-main a {
    cursor: not-allowed;
    pointer-events: none;
}

.programs_html .word-details-card .word-details-con .word {
    color: rgb(235, 98, 45) !important;
}

.programs_html .content-wrap {
    padding-top: 0 !important;
}

.programs_html #trans-input,
.programs_html #trans-result {
    color: rgb(134, 170, 204) !important;
    font-size: 16px !important;
}

.programs_html .input-area #trans-input {
    width: 100% !important;
    box-sizing: border-box;
    padding-left: 10px !important;
    height: auot !important;
}

.programs_html .input-area .btn-clear {
    display: none !important;
}
</style>"""

def main():
    if len(sys.argv) < 2:
        print("Usage: python script.py <url>")
        return

    # 从命令行参数获取 URL 并进行编码
    url = ' '.join(sys.argv[1:])
    encoded_url = quote(url, safe=':/?&=')

    try:
        html = get_page(encoded_url)
        result = chardet.detect(html)
        encoding = result['encoding']
        
        # 解码 HTML 内容
        decoded_html = unescape(html.decode(encoding, 'ignore'))

        # 将相对链接转换为绝对链接
        decoded_html = decoded_html.replace('href="//', 'href="https://')
        decoded_html = decoded_html.replace('src="//', 'src="https://')

        # 设置 stdout 为 UTF-8 编码
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
        
        # 直接打印获取的内容
        print(decoded_html)
        print(css)
    except urllib.error.HTTPError as e:
        print(f'Downloading the page {url} failed with error code {e.code}.')
    except Exception as e:
        print(f'An error occurred: {e}')

if __name__ == "__main__":
    main()
  1. 在GoldenDict中添加“程序类词典”,命令行格式为
python "/xxxx/xxxx/SogouTranslatorGD.py" "https://fanyi.sogou.com/text?keyword=%GDWORD%&transfrom=auto&transto=zh-CHS&model=general"