python爬虫实现中英翻译词典

yipeiwu_com6年前Python爬虫

本文实例为大家分享了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爬取当当、京东、亚马逊图书信息代码实例

注:1.本程序采用MSSQLserver数据库存储,请运行程序前手动修改程序开头处的数据库链接信息 2.需要bs4、requests、pymssql库支持 3.支持多线程 from...

Python爬虫之正则表达式的使用教程详解

Python爬虫之正则表达式的使用教程详解

正则表达式的使用 re.match(pattern,string,flags=0) re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回...

Python 用Redis简单实现分布式爬虫的方法

Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台。 连接数据库 注意:假设现有几台在同一局域网内的机器分别为Master和几个Slaver...

编写Python脚本抓取网络小说来制作自己的阅读器

编写Python脚本抓取网络小说来制作自己的阅读器

你是否苦恼于网上无法下载的“小说在线阅读”内容?或是某些文章的内容让你很有收藏的冲动,却找不到一个下载的链接?是不是有种自己写个程序把全部搞定的冲动?是不是学了 python,想要找点东...

Python爬虫实现使用beautifulSoup4爬取名言网功能案例

本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下: 爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,...