Beispiel #1
0
 def test_translate(self):
     results = translate('pretty', '再见')
     self.assertEqual(results, {'再见': 'goodbye', 'pretty': '漂亮的'})
Beispiel #2
0
 def test_diy_translate(self):
     results = translate('你好', '再见', word_from='zh-CHS', word_to='ja')
     self.assertEqual(results, {'你好': 'こんにちは', '再见': 'さようなら'})
Beispiel #3
0
"""
不使用代理设置
"""
from translateyoudao.translate import translate

# 支持多个词一起翻译,返回值为翻以前和翻译后的键值对字典,翻译失败的词对应的键值为False,大家请自行判断翻译成功与否
print(translate('hello', '再见'))
Beispiel #4
0
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
words = re.findall(p, text)

# 输入你购买的代理订单号
order_no = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 获得代理对象,只需获取一次代理对象就行了,它会自动实现每隔5秒刷新一次代理IP
proxies = get_proxies(get_proxy_ip, args=(order_no, ))
# 建议一次不要传入太多单词
print(translate(*words, proxies=proxies))
Beispiel #5
0
def write_files(item):
    url = item.get('url')
    req = request.Request(url, headers=headers)
    response = request.urlopen(req)
    bf = BeautifulSoup(response, 'html.parser')
    title = bf.find(class_='Content-title').contents
    # 多个 title 只选择第一个
    if len(title) > 1:
        title = ''.join(title[0])
    else:
        title = ''.join(title)
    # <title>标签也包含一个子节点:字符串 “The Dormouse’s story”,这种情况下字符串 “The Dormouse’s story”也属于<head>标签的子孙节点.
    description = bf.find(class_="Video-description").contents[1].contents
    # 多个 description 只选择第一个
    if len(description) > 1:
        description = ''.join(description[0])
    else:
        description = ''.join(description)
    pattern = re.compile(r'https?://video1.[^\s]*.mp4')  # 匹配网站内视频直链
    responsetext = request.urlopen(req).read().decode('utf-8')
    videolink = pattern.findall(responsetext)

    flag = 1  # 标识视频是否是 youtube 还是 网站视频直链.mp4
    if len(videolink) == 0:  # 如果是 YouTube 视频 则找出油管的链接,然后设置标志位
        pattern = re.compile(r'https?://www.youtube.com/embed[^\s]+rel=0')
        videolink = pattern.findall(responsetext)
        videolink = ''.join(videolink[0])
        flag = 0
    elif len(videolink) > 1:
        videolink = ''.join(videolink[0])
    else:
        videolink = ''.join(videolink)

    # 写入文件
    file1.write("\n")
    file1.write("初始链接: " + url + '\n')
    file1.write("标题: " + title + '\n')
    file1.write("谷歌翻译:" + ''.join(googletrans(title)) + '\n')
    # file1.write("有道翻译: "+ ''.join(youdao(title))+'\n')
    file1.write("有道翻译: " + ''.join(translate(title).get(title)) + '\n')
    file1.write("百度翻译: " + ''.join(baidu(title)) + '\n')
    file1.write("内容: " + description + "\n")
    file1.write("谷歌翻译: " + ''.join(googletrans(description)) + '\n')
    file1.write("有道翻译: " + ''.join(translate(description).get(description)) +
                '\n')
    file1.write("百度翻译: " + ''.join(baidu(description)) + '\n')
    file1.write("视频链接: " + ''.join(videolink) + "\n")
    file1.write("\n")

    # # 保存视频
    title = title.replace('?', '')  # 去掉文件名中的 特殊字符 ?
    path = 'video/' + title + '.mp4'
    if flag == 1:
        with open(path, 'ab') as ft:
            req = request.Request(videolink, headers=headers)
            ft.write(request.urlopen(req).read())
            ft.flush()
    else:
        ydl = youtube_dl.YoutubeDL()
        ydl_opts = {'proxy': 'socks5://127.0.0.1/', 'outtmpl': path}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download(videolink.split())
Beispiel #6
0
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
words = set(re.findall(p, text))

# 输入你购买的代理订单号
order_no = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 获得代理对象,只需获取一次代理对象就行了,它会自动实现每隔5秒刷新一次代理IP
proxies = get_proxies(get_proxy_ip, args=(order_no, ))
# 建议一次不要传入太多单词
begin = time()
res = translate(*words, proxies=proxies)
end = time()
print('请求接口走了代理,以及有重试机制,所以速度稍慢')
print('用时: {}秒'.format(int(end - begin)))
success = 0
failed = 0
print('单词数: {}个'.format(len(words)))
for v in res.values():
    if v is not False:
        success += 1
    else:
        failed += 1

print('成功: {}个'.format(success))
print('失败: {}个'.format(failed))