Python爬虫1-----------placekitten 入门

常用的urllib库有三个类:request,parse,error,request主要完成对url的请求,如proxy,opener,urlopen,parse主要完成对html的解析,error负责异常处理。

1 import urllib.request
2 import urllib.
3 
4 html=urllib.request.urlopen('http://placekitten.com/500/600')
5 print(type(html))  # 返回的response是一个网页类型的文件
6 img=html.read()     #img是二进制文件
7 print(type(img))
8 with open("c://spider/cat.jpg",'wb') as f:
9     f.write(img)

打开一个url,返回

<class 'http.client.HTTPResponse'>
<class 'bytes'>

原文地址:https://www.cnblogs.com/caojunjie/p/6725907.html