留学的时候学习了几年法语,回国后逐渐生疏,一个朋友说帮忙翻译一些东西,但还是有点吃力,想着前面研究的爬虫知识,能否自动完成翻译呢,话不多说,开整。
整体的爬虫思考可以参看之前的文章
12.奇怪知识(1) --Matlab爬虫获取王者荣耀英雄皮肤
首先用chrome打开谷歌搜翻译,看看它是怎么请求数据的
右键 检查 进入开发者工具
选择network,如图
先点击一下这个clear,把原来的请求都clear,免得看不清
输入测试翻译内容
通过请求头信息,可以发现是get请求,请求翻译的内容是通过q这个参数传递的,其它参数暂时不知道什么意思。
q这个参数不清楚谷歌的方法,但有大神出来,我们直接用
class Py4Js(): def __init__(self): self.ctx = execjs.compile(""" xo=function(a,b){ for(var c=0;c<b.length-2;c+=3) {var d=b.charAt(c+2);d="a"<=d?d.charCodeAt(0)-87:Number(d);d="+"==b.charAt(c+1)?a>>>d:a<<d;a="+"==b.charAt(c)?a+d&4294967295:a^d}return a}
function TL(a){ var wo=function(a){return function(){return a}} b=wo(String.fromCharCode(84)); var c=wo(String.fromCharCode(75)); b=[b(),b()];b[1]=c(); b="750.0"; var d=wo(String.fromCharCode(116)); c=wo(String.fromCharCode(107)); d=[d(),d()]; c="&"+d.join("")+ "="; d=b.split("."); b=6; for(var e=[],f=0,g=0;g<a.length;g++) {var k=a.charCodeAt(g);128>k?e[f++]=k:(2048>k?e[f++]=k>>6|192:(55296==(k&64512)&&g+1<a.length&&56320==(a.charCodeAt(g+1)&64512)?(k=65536+((k&1023)<<10)+(a.charCodeAt(++g)&1023),e[f++]=k>>18|240,e[f++]=k>>12&63|128):e[f++]=k>>12|224,e[f++]=k>>6&63|128),e[f++]=k&63|128)}a=b;for(f=0;f<e.length;f++)a+=e[f],a=xo(a,"+-a^+6");a=xo(a,"+-3^+b+-f");a^=Number(d[1])||0;0>a&&(a=(a&2147483647)+2147483648);a%=1E6; return c+(a.toString()+"."+ (a^b))} """)
def getTk(self, text): return self.ctx.call("TL", text)
根据上面得到的q参数拼接url,得到
def buildUrl(text, tk): baseUrl = "https://translate.google.cn/translate_a/single?client=webapp&sl=zh-CN&tl=fr&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&swap=1&otf=2&ssel=5&tsel=5&kc=1&" baseUrl += 'tk=' + str(tk) + '&' baseUrl += 'q=' + urllib.parse.quote(text) # print(baseUrl) return baseUrl
baseurl里面的tl控制翻译语言,例如中翻译英语,tl=en,我这儿是法语,所以tl=fr。
通过requests函数进行请求,得到翻译的返回结果
def translate(js, text): header = { 'cookie': 'NID=188=Nx_B7MPjOKKUBKu4LByiqdUEwcO4goXhVKB0vtqhvJycCD3TIPTgA7HU80AQ4LJXfrAjV8gvawvSDMKgS52MkV3JB44kgzNq9aHp41EuL8-2Cns1re4xCgQvPr1jMI9JPZxFU9fdHtymXto3qCv64HVBIkQ8vfBRMxKeZl0XS4g', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36' } url = buildUrl(text, js.getTk(text)) res = '' try: r = requests.get(url) result = json.loads(r.content.decode("utf-8")) res = result[0][0][0] except Exception as e: res = '' print(url) print("翻译失败:" + text) print(e) finally: return res
我这个数据全是在excel,所以数据从excel里面读取,最后再写回到excel,代码如下
def write_excel_xls(path, sheet_name, value): index = len(value) # 获取需要写入数据的行数 workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(sheet_name) # 在工作簿中新建一个表格 for i in range(0, index): for j in range(0, len(value[i])): sheet.write(i, j, value[i][j]) # 像表格中写入数据(对应的行和列) workbook.save(path) # 保存工作簿 print("xls格式表格写入数据成功!")
def write_excel_xls_append(path, value): index = len(value) # 获取需要写入数据的行数 workbook = xlrd.open_workbook(path) # 打开工作簿 sheets = workbook.sheet_names() # 获取工作簿中的所有表格 worksheet = workbook.sheet_by_name(sheets[0]) # 获取工作簿中所有表格中的的第一个表格 rows_old = worksheet.nrows # 获取表格中已存在的数据的行数 new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象 new_worksheet = new_workbook.get_sheet(0) # 获取转化后工作簿中的第一个表格 for i in range(0, index): for j in range(0, len(value[i])): new_worksheet.write(i + rows_old, j, value[i][j]) # 追加写入数据,注意是从i+rows_old行开始写入 new_workbook.save(path) # 保存工作簿 print("xls格式表格【追加】写入数据成功!")
if __name__ == '__main__': file_name = "test.xlsx" ad_wb = xlrd.open_workbook(file_name) # 获取第一个目标表单 sheet_0 = ad_wb.sheet_by_index(0) book_name_xls = file_name.split(".")[0] + '-翻译结果.xls' sheet_name_xls = '中法翻译结果' value_title = [["原文", "法文"], ] write_excel_xls(book_name_xls, sheet_name_xls, value_title) for r in range(0, sheet_0.nrows): # text1 = sheet_0.cell_value(r, 1) js1 = Py4Js() res1 = translate(js1, text1) print(r-2, text1, res1) value1 = [[text1, str(res1)], ] write_excel_xls_append(book_name_xls, value1)
运行后结果如图
完整代码如下:
import requestsimport urllib.parseimport jsonimport execjs # 可通过pip install PyExecJS安装,用来执行js脚本import xlrdimport xlwtfrom xlutils.copy import copy
class Py4Js():def __init__(self): self.ctx = execjs.compile(""" xo=function(a,b){ for(var c=0;c<b.length-2;c+=3) {var d=b.charAt(c+2);d="a"<=d?d.charCodeAt(0)-87:Number(d);d="+"==b.charAt(c+1)?a>>>d:a<<d;a="+"==b.charAt(c)?a+d&4294967295:a^d}return a}
function TL(a){ var wo=function(a){return function(){return a}} b=wo(String.fromCharCode(84)); var c=wo(String.fromCharCode(75)); b=[b(),b()];b[1]=c(); b="750.0"; var d=wo(String.fromCharCode(116)); c=wo(String.fromCharCode(107)); d=[d(),d()]; c="&"+d.join("")+ "="; d=b.split("."); b=6; for(var e=[],f=0,g=0;g<a.length;g++) {var k=a.charCodeAt(g);128>k?e[f++]=k:(2048>k?e[f++]=k>>6|192:(55296==(k&64512)&&g+1<a.length&&56320==(a.charCodeAt(g+1)&64512)?(k=65536+((k&1023)<<10)+(a.charCodeAt(++g)&1023),e[f++]=k>>18|240,e[f++]=k>>12&63|128):e[f++]=k>>12|224,e[f++]=k>>6&63|128),e[f++]=k&63|128)}a=b;for(f=0;f<e.length;f++)a+=e[f],a=xo(a,"+-a^+6");a=xo(a,"+-3^+b+-f");a^=Number(d[1])||0;0>a&&(a=(a&2147483647)+2147483648);a%=1E6; return c+(a.toString()+"."+ (a^b))} """)
def getTk(self, text): return self.ctx.call("TL", text)
def buildUrl(text, tk): baseUrl = "https://translate.google.cn/translate_a/single?client=webapp&sl=zh-CN&tl=fr&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&swap=1&otf=2&ssel=5&tsel=5&kc=1&" baseUrl += 'tk=' + str(tk) + '&' baseUrl += 'q=' + urllib.parse.quote(text) # print(baseUrl) return baseUrl
def translate(js, text): header = { 'cookie': 'NID=188=Nx_B7MPjOKKUBKu4LByiqdUEwcO4goXhVKB0vtqhvJycCD3TIPTgA7HU80AQ4LJXfrAjV8gvawvSDMKgS52MkV3JB44kgzNq9aHp41EuL8-2Cns1re4xCgQvPr1jMI9JPZxFU9fdHtymXto3qCv64HVBIkQ8vfBRMxKeZl0XS4g', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36' } url = buildUrl(text, js.getTk(text)) res = '' try: r = requests.get(url) result = json.loads(r.content.decode("utf-8")) res = result[0][0][0] except Exception as e: res = '' print(url) print("翻译失败:" + text) print(e) finally: return res
def write_excel_xls(path, sheet_name, value): index = len(value) # 获取需要写入数据的行数 workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(sheet_name) # 在工作簿中新建一个表格 for i in range(0, index): for j in range(0, len(value[i])): sheet.write(i, j, value[i][j]) # 像表格中写入数据(对应的行和列) workbook.save(path) # 保存工作簿 print("xls格式表格写入数据成功!")
def write_excel_xls_append(path, value): index = len(value) # 获取需要写入数据的行数 workbook = xlrd.open_workbook(path) # 打开工作簿 sheets = workbook.sheet_names() # 获取工作簿中的所有表格 worksheet = workbook.sheet_by_name(sheets[0]) # 获取工作簿中所有表格中的的第一个表格 rows_old = worksheet.nrows # 获取表格中已存在的数据的行数 new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象 new_worksheet = new_workbook.get_sheet(0) # 获取转化后工作簿中的第一个表格 for i in range(0, index): for j in range(0, len(value[i])): new_worksheet.write(i + rows_old, j, value[i][j]) # 追加写入数据,注意是从i+rows_old行开始写入 new_workbook.save(path) # 保存工作簿 print("xls格式表格【追加】写入数据成功!")
if __name__ == '__main__': file_name = "test.xlsx" ad_wb = xlrd.open_workbook(file_name) # 获取第一个目标表单 sheet_0 = ad_wb.sheet_by_index(0) book_name_xls = file_name.split(".")[0] + '-翻译结果.xls' sheet_name_xls = '中法翻译结果' value_title = [["原文", "法文"], ] write_excel_xls(book_name_xls, sheet_name_xls, value_title) for r in range(0, sheet_0.nrows): # text1 = sheet_0.cell_value(r, 1) js1 = Py4Js() res1 = translate(js1, text1) print(r, text1, res1) value1 = [[text1, str(res1)], ] write_excel_xls_append(book_name_xls, value1)
觉得不错的读友点个赞