第53课:
Python 3.x的urllib实际上是将python2.X的urllib,urllib2合并成一个包。
urllib实际上是一个包,包含以下四个模块:
urllib.request
urllib.error
urllib.parse
urllib.robotparser
7 | html=html.decode( 'UTF-8' ) |
不过第一次执行urllib.request.urlopen的时候,老是出现“ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host”的错误,不过第二天运行的时候又好了。
第54课:
下载猫咪图片的代码:
4 | cat_img = response.read() |
6 | with open( 'cat_200_300.jpg' , 'wb' ) as f: |
也可以写成:
5 | response = urllib.request.urlopen(req) |
6 | cat_img = response.read() |
8 | with open( 'cat_200_300.jpg' , 'wb' ) as f: |
小甲鱼写的调用有道api进行翻译的代码(PY3.X):不过只能翻译英文,不能翻译中文。
8 | self.key = '695028818' #有道API key |
9 | self.keyfrom = 'nicomochina' #有道keyfrom |
11 | def get_translation(self,words): |
12 | url = self.url + '?keyfrom=' + self.keyfrom + '&key=' +self.key + '&type=data&doctype=json&version=1.1&q=' + words |
13 | page = urllib.request.urlopen(url) |
14 | result = page.read().decode( "utf8" ) |
15 | json_result = json.loads(result) |
16 | json_result = json_result[ "translation" ] |
22 | msg = input( '请输入单词\句子(输入quit结束):' ) |
25 | youdao.get_translation(msg) |
原载:蜗牛博客
网址:http://www.snailtoday.com
尊重版权,转载时务必以链接形式注明作者和原始出处及本声明。