Reptile_urllib的基本使用
一.使用介绍
-
urllib.request.urlopen(url,data,timeout)
-
第一个参数url即为URL,是必须要传送的。第二个参数data是访问URL时要传送的数据,第 三个timeout是设置超时时间。
-
第二三个参数是可以不传送的,data默认为空None,timeout默认为 socket._GLOBAL_DEFAULT_TIMEOUT
-
response.read()
-
read()方法就是读取文件里的全部内容,返回bytes类型的内容
-
.decode()方法可以将bytes类型转换成str类型的内容
-
response.getcode()
-
返回 HTTP的响应码,成功返回200,403服务器页面出错,500服务器问题
-
response.geturl()
-
response.info()
二.实例
from urllib.request import urlopen
url = 'http://www.baidu.com'
rsp = urlopen(url)
print(rsp.read().decode()[:100])
print(rsp.getcode())
print(rsp.geturl())
print(rsp.info())