#python 如何访问互联网
#url 一般格式 protocol://hostname[;port]/path/[;parameters]
#[?query]#fragment
#lib
import urllib.request
response=urllib.request.urlopen("http://www.fishc.com")
html =response.read()
html= html.decode("utf-8")
print(html)
导入 urllib.request,打开网页读取,编码规则utf-8
import urllib.request
response=urllib.request.urlopen("http://placekitten.com/g/500/600")
cat_img=response.read()
#req=urllib.request.Request("http://placekitten.com/g/500/600")
#response =urllib.request.urlopen(req)
with open('cat_500_600.jpg','wb') as f:
f.write(cat_img)
这段代码是二进制位把一张图片存储起来。
response.geturl()
'http://placekitten.com/g/500/600'
print(response.info())
Date: Thu, 23 Jun 2016 11:42:12 GMT
Content-Type: image/jpeg
Content-Length: 26590
Connection: close
Set-Cookie: __cfduid=de6449c3832066b6394c536b5dcff0db81466682132; expires=Fri, 23-Jun-17 11:42:12 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: 2b77ace311ea22f4-LAX
response.getcode()
200 表示成功