• 完美修复google翻译失效的问题


    背景

    使用chrome的小伙伴应该都知道有个页面一键翻译,对于英语相当蹩脚的我来说灰常好用,然而…
    在这里插入图片描述
    2010年,谷歌拒绝同意审查其在中国的搜索结果后,撤出了在中国的搜索引擎业务。
    2017年,谷歌为中国用户推出了改进版的Google翻译应用。
    2022 google translate 退出中国市场


    google翻译最终还是退出了中国。

    解决方法

    手动干预配置DNS,将google translate解析到提供服务的地区来达到效果。
    使用以下脚本可以获取延迟较低的ip
    windows下有一个很好用的快速修改hosts文件的工具SwitchHosts。

    ip 源

    translate地址

    源代码

    对代码做了部分注释,各位看官慢用. 高手请忽略

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    '''
    @Project :dataTransfer
    @File :Google Translate.py
    @Author :ninesun
    @Date :2022/11/18 8:43
    @Desc:
    '''
    
    import os
    from concurrent.futures import ThreadPoolExecutor
    # from pyperclip import copy
    
    os.system('title 查找最佳的谷歌翻译IP')
    
    ipAndSpeed = []
    
    ips = '''
    142.250.4.90
    172.253.114.90
    172.217.203.90
    172.253.112.90
    142.250.9.90
    172.253.116.90
    142.250.97.90
    142.250.30.90
    142.250.111.90
    172.217.215.90
    142.250.11.90
    142.251.9.90
    108.177.122.90
    142.250.96.90
    142.250.100.90
    142.250.110.90
    172.217.214.90
    172.217.222.90
    142.250.31.90
    142.250.126.90
    142.250.10.90
    172.217.195.90
    172.253.115.90
    142.251.5.90
    142.250.136.90
    142.250.12.90
    142.250.101.90
    172.217.192.90
    142.250.0.90
    142.250.107.90
    172.217.204.90
    142.250.28.90
    142.250.125.90
    172.253.124.90
    142.250.8.90
    142.250.128.90
    142.250.112.90
    142.250.27.90
    142.250.105.90
    172.253.126.90
    172.253.123.90
    172.253.62.90
    142.250.98.90
    172.253.113.90
    '''
    
    
    def ipList():
        '''获取IP地址'''
        return { i.strip() for i in ips.splitlines() if i.strip() }
    
    
    def pingInfo(ip):
        '''ping Ip 获取ms 最终取最小值'''
        cmd = f'ping /n 1 {ip}'
        for echoTxt in os.popen(cmd):
            if '请求超时。' in echoTxt:
                ipAndSpeed.append([ip, 999])
                print(ip, '超时')
                return
            if echoTxt := echoTxt.strip(): # 去掉头尾的空格
                echoTxt = echoTxt.replace(' ', '') # 去掉字符串中间的空格,结合ping的结果来理解
                if ',平均=' in echoTxt:
                    ms = int(echoTxt.split('=')[-1].replace('ms', ''))  # 分割平均=xxms
                    ipAndSpeed.append([ip, ms])
                    print(ip, f'{ms}ms')
                    return
    
    
    def fastScan():
        with ThreadPoolExecutor(20) as Pool:  #使用线程池,设置20个线程,可修改
            Pool.map(pingInfo, ipList())
    
    
    fastScan()
    
    sortedSpeed = sorted(ipAndSpeed, key=lambda x: x[-1]) # 按照延迟大小升序排序
    for n, i in enumerate(sortedSpeed, 1): # 将一个sortedSpeed数据对象组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。 此例当中下标从1开始
        i[-1] = '超时' if i[-1] == 999 else f'{i[-1]}ms'
        print(f'【{str(n).zfill(2)}】\t{i[0]}\t {i[1]}') # zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。
    
    fastip, ms = sortedSpeed[0]
    print(f'\n最佳IP是:【{fastip}】,响应时间:【{ms}】')
    
    
    
    
    # copy(hostTxt)
    # print(f'\n\n设置hosts的内容“已复制到剪贴板”:   {hostTxt}\n\n\n按【任意键】打开hosts目录,然后【手动】修改。',
    #       end='')
    #
    # os.system('pause>nul')
    # os.popen('explorer /select,C:\Windows\System32\drivers\etc\hosts')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    技术点

    这个代码逻辑比较简单,你先ping一个ip看看结果再来理解这个代码。
    在这里插入图片描述

    python多线程

    ThreadPoolExecutor

    海象运算

    这里我的py环境是3.8.6 ,这个语法idea报错,但是代码可以运行。怀疑是Pycharm(2017.2.3)的bug.

      if echoTxt := echoTxt.strip(): # 头尾空格去掉
                echoTxt = echoTxt.replace(' ', '') # 正在 Ping 142.250.4.90 具有 32 字节的数据  把这句话的空格替换掉
                if ',平均=' in echoTxt:
                    ms = int(echoTxt.split('=')[-1].replace('ms', ''))  # 分割平均=xxms
                    ipAndSpeed.append([ip, ms])
                    print(ip, f'{ms}ms')
                    return
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    完美解决

    1、延迟较低的ip
    【01】	142.250.4.90	 86ms
    【02】	142.250.101.90	 184ms
    【03】	142.250.107.90	 207ms
    【04】	142.250.30.90	 210ms
    【05】	142.250.126.90	 212ms
    【06】	142.250.100.90	 213ms
    【07】	142.250.8.90	 213ms
    【08】	172.217.195.90	 216ms
    【09】	142.250.28.90	 218ms
    【10】	172.217.214.90	 222ms
    【11】	142.250.10.90	 223ms
    【12】	142.250.136.90	 223ms
    【13】	142.250.125.90	 224ms
    【14】	142.250.128.90	 226ms
    【15】	172.217.215.90	 228ms
    【16】	142.250.96.90	 229ms
    【17】	172.253.124.90	 230ms
    【18】	172.253.113.90	 230ms
    【19】	172.253.62.90	 231ms
    【20】	142.250.12.90	 231ms
    【21】	172.253.123.90	 233ms
    【22】	142.250.9.90	 234ms
    【23】	172.253.126.90	 234ms
    【24】	108.177.122.90	 234ms
    【25】	172.253.115.90	 237ms
    【26】	172.253.112.90	 237ms
    【27】	142.250.11.90	 238ms
    【28】	172.253.114.90	 239ms
    【29】	172.217.222.90	 240ms
    【30】	142.250.31.90	 242ms
    【31】	142.250.105.90	 243ms
    【32】	142.250.98.90	 243ms
    【33】	172.217.203.90	 246ms
    【34】	142.250.112.90	 246ms
    【35】	142.250.97.90	 253ms
    【36】	142.250.111.90	 253ms
    【37】	172.217.204.90	 253ms
    【38】	172.217.192.90	 296ms
    【39】	142.250.0.90	 299ms
    【40】	142.251.5.90	 307ms
    【41】	142.250.110.90	 307ms
    【42】	142.250.27.90	 311ms
    【43】	142.251.9.90	 311ms
    【44】	172.253.116.90	 323ms
    
    最佳IP是:【142.250.4.90】,响应时间:【86ms】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    2、配置hosts文件,

    如果有Switchhosts比较方便,没有的话打开 C:\Windows\System32\drivers\etc\hosts 修改同样可行.

    在这里插入图片描述

    3、翻译结果

    142.250.4.90 这个ip最快延迟86ms.
    在这里插入图片描述

    参考

    1、字符串语法
    2、海象语法

  • 相关阅读:
    JavaScript constructor&原型&原型继承
    国际公认—每个领导者必须拥抱的11项领导力转变
    Java基础this关键字02
    不能创建第三个变量,实现两个数的交换
    RabbitMQ - 06 - Topic交换机
    周热点回顾(6.13-6.19)
    开发基于 ChatGPT 分析热点事件并生成文章的网站应用【热点问天】把百度等热点用chatGPT来对热点事件分析海量发文章 开发步骤 多种方式获取利润
    前台自动化测试:基于敏捷测试驱动开发(TDD)的自动化测试原理
    [LeetCode周赛复盘] 第 361 场周赛20230906
    【算法基础】动态规划
  • 原文地址:https://blog.csdn.net/MyySophia/article/details/127777533