1 Star 0 Fork 0

关机闹钟/PythonSpiders

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fanyi.py 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
关机闹钟 提交于 2018-05-28 08:47 . 更新
import requests
import json
class BaiduTranslator:
def __init__(self):
self.headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"}
def langdetect(self, words):
url = 'http://fanyi.baidu.com/langdetect'
data = {
'query': words
}
response = requests.post(url, data=data)
lang_info = json.loads(response.content.decode())
return lang_info['lan']
def translator(self, words):
url = 'http://fanyi.baidu.com/basetrans'
data = {
'query': words,
'from': self.langdetect(words)
}
data['to'] = 'zh' if data['from'] == 'en' else 'en'
response = requests.post(url, data=data, headers=self.headers)
lang_info = response.json()
return lang_info['trans'][0]['dst']
class YoudaoTranslator:
def __init__(self):
self.headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"}
def translator(self, words):
url = 'http://m.youdao.com/translate'
data = {
'inputtext': words,
'type': 'AUTO'
}
response = requests.post(url, data=data, headers=self.headers)
html_str = response.content.decode()
ul_start = '<ul id="translateResult">'
li_start = '<li>'
li_end = '</li>'
ul_start_pos = html_str.find(ul_start) + len(ul_start)
li_start_pos = html_str.find(li_start, ul_start_pos) + len(li_start)
li_end_pos = html_str.find(li_end, ul_start_pos)
print(html_str[li_start_pos:li_end_pos])
class CibaTranslator:
def __init__(self):
self.headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"}
def translator(self, words):
url = 'http://fy.iciba.com/ajax.php?a=fy'
data = {
'f': 'auto',
't': 'auto',
'w': words
}
response = requests.post(url, data=data, headers=self.headers)
info = response.json()
print(info['content']['out'])
if __name__ == '__main__':
youdao = YoudaoTranslator()
youdao.translator('测试')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/shiyilin/PythonSpiders.git
[email protected]:shiyilin/PythonSpiders.git
shiyilin
PythonSpiders
PythonSpiders
master

搜索帮助