英文不好一直是我的一个短板,尤其是在学习代码的阶段,经常需要查询各种错误,很是苦逼,一直就想自己做个翻译的脚本,省去打开网页的时间,但是查询之后发现网上的教程都是百度翻译改版之前的爬虫,不得已只好自己上手了!
目标:制作自己的翻译脚本
url: http://fanyi.baidu.com/basetrans
前期准备:pycharm、python3.6、库:requests、json
思路:
Lan就是抓包后返回的内容,直接提取出来
{"User-Agent":"Mozilla/5.0(Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/63.0.3239.84 Mobile Safari/537.36"}
比如输入“python学习交流群: 542110741”后返回内容:
Josn内容如下:
{'errno': 0, 'from': 'zh', 'to': 'en','trans': [{'dst': 'Python learning communication group: 542110741','prefixWrap': 0, 'src': 'python学习交流群: 542110741', 'relation': [], 'result': [[0, 'Python learningcommunication group: 542110741', ['0|32'], [], ['0|32'], ['0|46']]]}], 'dict':[], 'keywords': [{'means': ['巨蛇,大蟒'], 'word': 'python'}, {'means': ['study', 'learn', 'emulate','learning'], 'word': '学习'}, {'means': ['exchange', 'interflow', 'interchange','alternating', 'AC (alternating current)', 'communion'], 'word': '交流'}]}
我们可以分别抓取'trans'和'keywords'的值,我们所需要的内容,就在这两个值里。
好了,直接上代码和运行结果
好了,内容到这里就完了~如果对大家有帮助,就关注下呗
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有