1.robots协议
一般协议,不具有强制性
域名/robots.txt 查看
2.requests模块
是个第三方模块,需要安装 pip install requests -i https://pypi.doubanio.com/simple
利用requests模块发送网络请求 接收请求数据
3. 响应体操作
一般用content得到二进制数据,再decode,text能直接得到字符串数据,但是是自动解码,不一定准确
response.text 响应体 str类型 respones.content 响应体 bytes类型 response.status_code 响应状态码 response.request.headers 响应对应的请求头 response.headers 响应头 response.request._cookies 响应对应请求的cookie response.cookies 响应的cookie(经过了4.set-cookie动作)
4.with open as 语句操作文件读写
(1)如果得到的字符串数据,用w模式打开,需要设置编码格式encoding='utf-8'(具体编码要看网页所用的编码,在网页检查-elements-head标签下可以看到
with open('baidu.html','w',encoding="utf-8") as f: # f.write(response_.text) (2)如果以二进制数据写入的话,用wb模式,就不需要设置编码了 (比如图片,音频,视频等)