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

response的类型是HTTPResponse
read()方法
read():一个字节一个字节去读取,直到读完。
read(5):读取5个字节

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

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

需要注意的是,readlines()读出来的是一个list。
list对象是没有decode属性的。

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

返回状态码
geturl()方法
获取请求的url

getheaders()方法
返回响应头

[('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')]

乖乖学习,好好做事。

浙公网安备 33010602011771号