前言

学了这么久的python理论知识,需要开始实战来练手巩固了。

准备

首先安装爬虫urllib库

pip install urllib

获取有道翻译的链接url



需要发送的参数在form data里

示例

import urllib.request
import urllib.parse url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
data = {}
data['i'] = 'i love python'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16057996372935'
data['sign'] = '0965172abb459f8c7a791df4184bf51c'
data['lts'] = '1605799637293'
data['bv'] = 'f7d97c24a497388db1420108e6c3537b'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
print(html)

运行会出现50的错误,这里需要将url链接的_o删除掉



删除后运行成功



但是这个结果看起来还是太复杂,需要在进行优化

导入json,然后转换成字典进行过滤

import urllib.request
import urllib.parse
import json url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = 'i love python'
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16057996372935'
data['sign'] = '0965172abb459f8c7a791df4184bf51c'
data['lts'] = '1605799637293'
data['bv'] = 'f7d97c24a497388db1420108e6c3537b'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8') req = json.loads(html)
result = req['translateResult'][0][0]['tgt']
print(result)



但是这个程序只能翻译一个单词,用完就废了。于是我在进行优化

import urllib.request
import urllib.parse
import json def translate():
centens = input('输入要翻译的语句:')
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head = {}#增加请求头,防反爬虫
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
data = {}#带上from data的数据进行请求
data['i'] = centens
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16057996372935'
data['sign'] = '0965172abb459f8c7a791df4184bf51c'
data['lts'] = '1605799637293'
data['bv'] = 'f7d97c24a497388db1420108e6c3537b'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTlME'
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')
req = json.loads(html)
result = req['translateResult'][0][0]['tgt']
# print(f'中英互译的结果:{result}')
return result
t = translate()
print(f'中英互译的结果:{t}')

优化完成,效果还行。

Python爬虫实现翻译功能的更多相关文章

  1. Python爬虫教程-06-爬虫实现百度翻译(requests)

    使用python爬虫实现百度翻译(requests) python爬虫 上一篇介绍了怎么使用浏览器的[开发者工具]获取请求的[地址.状态.参数]以及使用python爬虫实现百度翻译功能[urllib] ...

  2. Python爬虫教程-05-python爬虫实现百度翻译

    使用python爬虫实现百度翻译功能 python爬虫实现百度翻译: python解释器[模拟浏览器],发送[post请求],传入待[翻译的内容]作为参数,获取[百度翻译的结果] 通过开发者工具,获取 ...

  3. Python爬虫爬取百度翻译之数据提取方法json

    工具:Python 3.6.5.PyCharm开发工具.Windows 10 操作系统 说明:本例为实现输入中文翻译为英文的小程序,适合Python爬虫的初学者一起学习,感兴趣的可以做英文翻译为中文的 ...

  4. python爬虫学习---爬取微软必应翻译(中英互译)

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:OSinooO 本人属于python新手,刚学习的 python爬虫基础 ...

  5. Python爬虫教程-实现百度翻译

    使用python爬虫实现百度翻译功能python爬虫实现百度翻译: python解释器[模拟浏览器],发送[post请求],传入待[翻译的内容]作为参数,获取[百度翻译的结果] 通过开发者工具,获取发 ...

  6. python爬虫高级功能

    上一篇文章中我们介绍了爬虫的实现,及爬虫爬取数据的功能,这里会遇到几个问题,比方站点中robots.txt文件,里面有禁止爬取的URL.还有爬虫是否支持代理功能.及有些站点对爬虫的风控措施.设计的爬虫 ...

  7. Python爬虫教程-08-post介绍(百度翻译)(下)

    Python爬虫教程-08-post介绍(下) 为了更多的设置请求信息,单纯的通过urlopen已经不太能满足需求,此时需要使用request.Request类 构造Request 实例 req = ...

  8. Python爬虫教程-07-post介绍(百度翻译)(上)

    Python爬虫教程-07-post介绍(百度翻译)(上) 访问网络两种方法 get: 利用参数给服务器传递信息 参数为dict,使用parse编码 post :(今天给大家介绍的post) 一般向服 ...

  9. Python爬虫教程-16-破解js加密实例(有道在线翻译)

    python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...

随机推荐

  1. PPT神器

    今天要给大家推荐一款开挂一般的 PPT 插件:iSlide 强烈推荐大家下载使用哈,绝对分分钟让你做出美观大气的 PPT!      不管是老师.学生还是公司人员,PPT 都是必须要掌握的技能,然而要 ...

  2. windows7 安装配置NodeJS、NPM

    转载自https://blog.csdn.net/dengxw00/article/details/82974808 windows7 安装配置NodeJS.NPM一.安装 NodeJS1.登陆官网( ...

  3. SpringMVC的@InitBinder参数转换

    @Controller @RequestMapping("/index") public class IndexController { /** * 解决前端传递的日期参数验证异常 ...

  4. centos7安装mongodb4.0教程

    1.配置软件仓库: vim /etc/yum.repos.d/mongodb-org-4.0.repo [mongodb] name=MongoDB baseurl=https://repo.mong ...

  5. NB-IoT和LORA技术通信距离是一样的吗

    如今物联网的无线通信技术非常多,这其中主要分为两大类:一类是以Zigbee.WiFi.蓝牙.Z-wave等短距离通信技术为主:另一类就是以LPWAN(low-powerWide-AreaNetwork ...

  6. Learn day6 模块pickle\json\random\os\zipfile\面对对象(类的封装 操作 __init__)

    1.模块 1.1 pickle模块 # ### pickle 序列化模块 import pickle """ 序列化: 把不能够直接存储的数据变得可存储 反序列化: 把数 ...

  7. 华为云FusionInsight MRS:助力企业构建“一企一湖,一城一湖”

    摘要:华为云FusionInsight MRS新一代的数据湖,让大数据越用越快.越用越易.越用越稳.越用越省!让数据价值近在眼前! 10月30日,以"携手共赢·数创未来"为主题的第 ...

  8. 【漏洞复现】Shiro<=1.2.4反序列化漏洞

    0x01 概述 Shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从 ...

  9. pycharm新建项目时选择virtualenv的说明

    虚拟环境及venv和virtualenv介绍:https://www.cnblogs.com/mind18/p/13877170.html pip介绍:https://www.cnblogs.com/ ...

  10. leetcode73:minmum-window-substring

    题目描述 给出两个字符串S和T,要求在O(n)的时间复杂度内在S中找出最短的包含T中所有字符的子串. 例如: S ="ADOBECODEBANC" T ="ABC&quo ...