使用urllib.request模块中的urlopen()方法发送http请求
import urllib
response=urllib.request.urlopen('http://www.baidu.com') # 服务器返回的响应内容封装在HTTPResponse类型的对象中
print(type(response)) # HTTPResponse对象
print('*'*20)
print(response.geturl())
print(response.getcode())
print('*'*20)
print(response.info())
输出结果:
<class 'http.client.HTTPResponse'> # HTTPResponse对象
********************
http://www.baidu.com # 响应内容的url
200 # 响应状态码
********************
Bdpagetype: 1 #
Bdqid: 0x984c20ad00000333
Cache-Control: private #
Content-Type: text/html
Cxy_all: baidu+1d6a4aac9221bd30453985ab4dacc770
Date: Tue, 10 Sep 2019 06:17:25 GMT
Expires: Tue, 10 Sep 2019 06:16:47 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Server: BWS/1.1
Set-Cookie: BAIDUID=10437DB0757D58D32D51A5CBF09CBF31:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=10437DB0757D58D32D51A5CBF09CBF31; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1568096245; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: delPer=0; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1438_21124_18560_29523_29520_29721_29567_29220_26350; path=/; domain=.baidu.com
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
Connection: close
Transfer-Encoding: chunked