-
requset.urlopen(url,data,timeout)
- 第一个参数url即为URL,是必须要传送的。
- 第二个参数data是访问URL时要传送的数据,第三个timeout是设置超时时间。
- 第二三个参数是可以不传送的,data默认为空None,timeout默认为
socket._GLOBAL_DEFAULT_TIMEOUT
-
response.read()
- read()方法就是读取文件里的全部内容,返回bytes类型
-
response.getcode()
- 返回 HTTP的响应码,成功返回200,4服务器页面出错,5服务器问题
-
response.geturl()
- 返回 返回实际数据的实际URL,防止重定向问题(该问题在后续会遇到!)
-
response.info()
- 返回 服务器响应的HTTP报头
- 代码演示
from urllib.request import urlopen
url = 'http://www.baidu.com/'
resp = urlopen(url)
print(resp.read().decode()[:200])
print(resp.getcode())
print(resp.geturl())
print(resp.info())