Python下载一张图片与有道词典
1.下载一张图片代码1
import urllib.request
response = urllib.request.urlopen('http://photocdn.sohu.com/20100906/Img274741430.jpg')
image = response.read()
with open('D:\\cat_200_300.jpg','wb') as f: #打开文件
f.write(image) #写入文件
2.下载一张图片代码2
import urllib.request
request = urllib.request.Request('http://photocdn.sohu.com/20100906/Img274741430.jpg')
response = urllib.request.urlopen(request)
image = response.read()
with open('D:\\cat_200_300.jpg','wb') as f:
f.write(image)
3.获取请求的url
> request.get_full_url()
'http://photocdn.sohu.com/20100906/Img274741430.jpg
4.获取响应码
response.getcode()
200
5.获取响应信息
response.info()
<http.client.HTTPMessage object at 0x03015850>
>>> print(response.info())
Server: Tengine
Content-Type: image/jpeg
Content-Length: 32052
Connection: close
Date: Wed, 11 Oct 2017 02:59:56 GMT
Last-Modified: Mon, 06 Sep 2010 08:27:47 GMT
Expires: Tue, 09 Jan 2018 02:59:56 GMT
Cache-Control: max-age=7776000
X-RS: 34079416.44499832.45112407
X-RS: 10888362.20063412.11942198
FSS-Cache: MISS from 6562920.11412594.7616690
FSS-Proxy: Powered by 6497640.11281778.7551665
Accept-Ranges: bytes
Via: cache14.l2et15-2[0,200-0,H], cache18.l2et15-2[1,0], cache2.cn869[80,200-0,M], cache9.cn869[81,0]
Age: 7118675
X-Cache: MISS TCP_MISS dirn:-2:-2 mlen:-1
X-Swift-SaveTime: Mon, 01 Jan 2018 12:24:31 GMT
X-Swift-CacheTime: 657325
Timing-Allow-Origin: *
EagleId: 767b029d15148094715568685e
6.访问有道词典查询单词
import urllib.request
import urllib.parse url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = 'I love you'
data['type'] = 'AUTO'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['typoResult'] = 'true' data = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.urlopen(url,data) html = response.read().decode('utf-8') print(html)
7.结果json数据
{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"I love you","tgt":"我爱你"}]]}
8.处理查询的数据,将json转化为字典
import json
target = json.loads(html)
{'type': 'EN2ZH_CN', 'errorCode': 0, 'elapsedTime': 1, 'translateResult': [[{'src': 'I love you', 'tgt': '我爱你'}]]}
9.获取翻译的数据
target['translateResult'][0][0]['tgt']
10.有道词典
import urllib.request
import urllib.parse
import json while True:
text = input('请输入要翻译的文字:')
if text == 'exit':
break url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = text
data['type'] = 'AUTO'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['typoResult'] = 'true' data = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.urlopen(url,data) html = response.read().decode('utf-8') target = json.loads(html) print(target['translateResult'][0][0]['tgt'])
print('输入exit退出程序!')
'我爱你'
代理

Python下载一张图片与有道词典的更多相关文章
- 如何用python下载一张图片
如何用python下载一张图片 这里要用到的主要工具是requests这个工具,需要先安装这个库才能使用,该库衍生自urllib这个库,但是要比它更好用.多数人在做爬虫的时候选择它,是个不错的选择. ...
- python学习笔记:"爬虫+有道词典"实现一个简单的英译汉程序
1.有道的翻译 网页:www.youdao.com Fig1 Fig2 Fig3 Fig4 再次点击"自动翻译"->选中'Network'->选中'第一项',如下: F ...
- Ubuntu14.04安装有道词典
Ubuntu14.04安装有道词典之前要更新系统: sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade 在有道官网下载 ...
- 调用网易有道词典api
# -*- coding: utf-8 -*- #python 27 #xiaodeng #调用网易有道词典api import urllib import json class Youdao(): ...
- python:爬虫1——实战(下载一张图片、用Python模拟浏览器,通过在线的有道词典来对文本翻译)
一.下载一只猫 import urllib.request response = urllib.request.urlopen("http://cdn.duitang.com/uploads ...
- python "爬虫+有道词典"实现一个简单翻译程序
抓包软件使用的是Fiddler4 新版的查询接口 比较负责,引入了salt和sign http://fanyi.youdao.com/translate?smartresult=dict&sm ...
- 使用python拼接多张图片.二三事
前几日在博客上看到一篇“使用python拼接多张图片”的Blog[具体是能将的图片名字必须是形如xx_1.png ... xx_100.png或者xx_001.png ... xx_100.png,拼 ...
- 基于pygtk的linux有道词典
基于pygtk的linux有道词典 一.桌面词典设计 想把Linux用作桌面系统,其中一部分障碍就是Linux上没有像有道一样简单易用的词典.其实我们完全可以自己开发一款桌面词典, 而且开发一款桌面词 ...
- ubuntu14.04 下安装有道词典
安装步骤 1.ubuntu14.04.1版本下是不能直接安装有道词典的,首先需要把14.04.版升级为14.04.2版. 在终端窗口中输入以下命令: sudo apt-get update sudo ...
随机推荐
- Python crawler access to web pages the get requests a cookie
Python in the process of accessing the web page,encounter with cookie,so we need to get it. cookie i ...
- 这些工具对html5开发有很大帮助
如今H5已经在IT这块很热门,所以也就有越来越多的人自学或是报名培训班学习H5,今天写一篇关于当下html5开发工具有哪些?哪个更好一些? 浅谈2017年html5开发工具哪个好: 1.Adobe D ...
- params SqlParameter[] commandParameters(转)
C#代码 ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...
- REMOTE_ADDR,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR获取客户端IP
话不多说,先上代码: /** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { ...
- 关于IDEA无法引入包和类的情况
原因:修改一些包结构后,会出现无法将类引入的情况. 通过清理缓存解决:
- 四、Hadoop学习笔记————各种工具用法
hive基本hql语法 Sqoop(发音:skup)是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库 ...
- c# AutoMapper 使用方式
安装方式:使用vs自带的nuget管理工具,搜索AutoMapper ,选择第一个安装到你的项目即可. 我从网上找了一些资料,下载了个demo,然后自己又写了一遍,我把AutoMapper 的使用分为 ...
- Android7.0 PowerManagerService 之亮灭屏(一)
本篇从按下power按键后,按键事件从InputManagerService 传到PhoneWindowManager.java开始分析power 按键做屏幕亮灭过程的分析,关于power 按键的其他 ...
- macbookPro 搭建maven环境下载jar包
今天要用itext的jar包,去官网看发现好像只能用maven下载,而我之前又换了电话,没办法咯,重新搭一次maven环境吧,在此记录,已便分享或自己将来查找 首选确定自己环境上jdk装好了,如果没有 ...
- Zepto中的Swipe事件失效
需要阻止浏览器默认滑动的事件 document.addEventListener('touchmove', function (event) { event.preventDefault(); }, ...