爬虫_054_urllib的1个类型和6个方法

1个类型

image-20240212183950031

response的类型是HTTPResponse

read()方法

read():一个字节一个字节去读取,直到读完。

read(5):读取5个字节

image-20240212184229404

readline()方法

readline()方法就是读取一行。

image-20240212184343376

readlines()方法

readlines()方法的意思是:一行一行地去读取,直到读完。

image-20240212184521582

需要注意的是,readlines()读出来的是一个list。

list对象是没有decode属性的。

image-20240212184619333

从上面的图片,可以很清楚看到,readlines()读出来的是list对象。

getcode()方法

image-20240212184744365

返回状态码

geturl()方法

获取请求的url

image-20240212184814047

getheaders()方法

返回响应头

image-20240212184840510

[('Connection', 'close'), 
('Content-Length', '404954'), 
('Content-Security-Policy', "frame-ancestors 'self' https://chat.baidu.com http://mirror-chat.baidu.com https://fj-chat.baidu.com https://hba-chat.baidu.com https://hbe-chat.baidu.com https://njjs-chat.baidu.com https://nj-chat.baidu.com https://hna-chat.baidu.com https://hnb-chat.baidu.com http://debug.baidu-int.com;"), 
('Content-Type', 'text/html; charset=utf-8'), 
('Date', 'Mon, 12 Feb 2024 10:48:23 GMT'), 
('P3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), 
('P3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), 
('Server', 'BWS/1.1'), 
('Set-Cookie', 'BAIDUID=D48F970912F21D3738803143CDB24906:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), 
('Set-Cookie', 'BIDUPSID=D48F970912F21D3738803143CDB24906; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), 
('Set-Cookie', 'PSTM=1707734903; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), 
('Set-Cookie', 'BAIDUID=D48F970912F21D37D5E87640D9EB655E:FG=1; max-age=31536000; expires=Tue, 11-Feb-25 10:48:23 GMT; domain=.baidu.com; path=/; version=1; comment=bd'), 
('Traceid', '1707734903058903015411385749501895298797'), 
('Vary', 'Accept-Encoding'), 
('X-Ua-Compatible', 'IE=Edge,chrome=1'), 
('X-Xss-Protection', '1;mode=block')]

image-20240212183815920

posted @ 2024-02-12 18:53  准提弟子  阅读(25)  评论(0)    收藏  举报