马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 游戏小虾米 于 2017-8-8 13:14 编辑
Tip:
一,理论
二,应用
1 下载一只猫import urllib.request
req = urllib.request.Request('http://placekitten.com/g/500/600')
response = urllib.request.urlopen(req)
# equal req = urllib.request.urlopen('http://placekitten.com/g/500/600')
cat_img = response.read()
with open('cat_500_600', 'wb') as f:
f.write(cat_img)
2
>>> response.geturl()
'http://placekitten.com/g/500/600'
>>> response.info()
<http.client.HTTPMessage object at 0x02F39350>
>>> print(response.info())
Date: Tue, 08 Aug 2017 03:11:23 GMT
Content-Type: image/jpeg
Content-Length: 26590
Connection: close
Set-Cookie: __cfduid=d5ee3088fc458f6434872dc702cdf51f81502161882; expires=Wed, 08-Aug-18 03:11:22 GMT; path=/; domain=.placekitten.com; HttpOnly
Accept-Ranges: bytes
X-Powered-By: PleskLin
Access-Control-Allow-Origin: *
Cache-Control: public
Expires: Thu, 31 Dec 2020 20:00:00 GMT
Server: cloudflare-nginx
CF-RAY: 38af49b7715a329b-HKG
>>> response.getcode() #获得状态 200 OK
200
2 有道词典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'] = '1502162456644'
data['sign'] = '4799f56048fcfc3308d676668a3afd56'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLlCKBUTTON'
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']))
三,课后练习 |