上篇文章给大家介绍了Python爬虫实现百度翻译功能过程详解
Python爬虫学习之翻译小程序 感兴趣的朋友点击查看。
今天给大家介绍Python爬虫制作翻译程序的方法,具体内容如下所示:
此处我爬的是百度翻译,打开百度翻译的页面
我们要爬的是sug,爬它的响应信息
程序如下
import json import requests if __name__ == "__main__": url = "https://fanyi.baidu.com/sug" header = { "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68' } while 1: a = input("输入单词(中文):") data = { "kw": a } res = requests.post(url=url, data=data, headers=header) data = json.loads(res.text) result = [(d['k'], d['v']) for d in data['data']] print("具体意思: "+data['data'][0]['v']) print("其他词性拓展: "+data['data'][1]['k']+" "+data['data'][1]['v']) for i in range(2, len(result)): print(" "+data['data'][i]['k']+" "+data['data'][i]['v'])