实例一:使用Python下载图片
import urllib.request response = urllib.request.urlopen('http://placekitten.com/g/500/600') cat_img = response.read() with open('cat_500_600.jpg', 'wb') as f: f.write(cat_img)
实例二:使用有道词典自动翻译
# http://bbs.fishc.com/thread-86581-1-1.html import urllib.request import urllib.parse import json content = input('请输入需要翻译的内容:') url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=' data = {} data['i'] = content data['from'] = 'AUTO' data['to'] = 'AUTO' data['smartresult'] = 'dict' data['client'] = 'fanyideskweb' data['salt'] = '1510557149618' #时间戳 data['sign'] = '81be643e4248fd97469f341c4b722605' #加密算法 data['doctype'] = 'json' data['version'] = '2.1' data['keyfrom'] = 'fanyi.web' data['action'] = 'FY_BY_CLICKBUTTION' 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('翻译结果:%s' % (target['translateResult'][0][0]['tgt']))