圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了python爬虫实现中英翻译词典的具体代码,供大家参考,具体内容如下

通过根据某平台的翻译资源,提取出翻译信息,并展示出来,包括输入,翻译,输出三个过程,主要利用python语言实现(python3.6),抓取信息展示。

import urllib.request
import urllib.parse
import json

def en_zh(content):
  url = 'http://fanyi.baidu.com/v2transapi'
  head = {}
  head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
  
  data={}
  data['from'] = 'en'
  data['to'] = 'zh'
  data['query'] = content
  data['transtype'] = 'translang'
  data['simple_means_flag'] = '3'
  data = urllib.parse.urlencode(data).encode('utf-8')

  req =urllib.request.Request(url,data,head)
  response=urllib.request.urlopen(req)

  html = response.read().decode('utf-8')

  target = json.loads(html)
  print("翻译结果:%s" %(target['trans_result']['data'][0]['dst']))
def zh_en(content):

  url = 'http://fanyi.baidu.com/v2transapi'
  data={}
  data['from'] = 'zh'
  data['to'] = 'en'
  data['query'] = content
  data['transtype'] = 'translang'
  data['simple_means_flag'] = '3'
  data = urllib.parse.urlencode(data).encode('utf-8')

  req =urllib.request.Request(url,data)
  req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
  response=urllib.request.urlopen(req)

  html = response.read().decode('utf-8')
  target = json.loads(html)
  print("翻译结果:%s" %(target['trans_result']['data'][0]['dst']))

while(True):
  content = input("请输入要翻译的内容(按q退出):")
  if content=='q':
    input("您已退出,欢迎再次使用")
    break

  en_zh(content) 
  zh_en(content)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python爬虫中英翻译词典,python翻译词典,python中英翻译词典

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com