做阿里巴巴网站口碑,wordpress分类缩略图,做网站单位,网络营销的基本方式有哪些爬虫入门之爬虫原理以及请求响应
爬虫需要用到的库, 叫requests.
在导入requests库之前, 需要安装它, 打开cmd: 输入pip install 库名
pip install requests后面出现successful或requirement already就说明已经下载成功了!!! 下载出现的问题: 1.有报错或者是下载慢 修改镜像…爬虫入门之爬虫原理以及请求响应
爬虫需要用到的库, 叫requests.
在导入requests库之前, 需要安装它, 打开cmd: 输入pip install 库名
pip install requests后面出现successful或requirement already就说明已经下载成功了!!! 下载出现的问题: 1.有报错或者是下载慢 修改镜像(从国内的仓库下载) 一、临时修改: pip install 库名 -i 国内仓库地址 pip install requests -i https://mirrors.aliyun.com/pypi/simple/ 二、永久修改 pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/ pip install requests 导入第三方库
下载好requests第三方库之后, 我们需要导入第三方库
import requestsget请求(获取百度网站的响应):
url = 'http://www.baidu.com'
res = requests.get(url)
# 获取响应输出的时候, 响应数据中有乱码
# 解决办法: 在输出内容之前, 设置响应编码
# 响应对象.encoding = '检查内容中charset的值'
res.encoding = "utf-8"
print(res.text)
# 关闭请求, 不关闭会导致资源浪费
res.close()控制台输出:
!DOCTYPE html
!--STATUS OK--html headmeta http-equiv=content-type content=text/html;charset=utf-8meta http-equiv=X-UA-Compatible content=IE=Edgemeta content=always name=referrerlink rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.csstitle百度一下,你就知道/title/head body link=#0000cc div id=wrapper div id=head div class=head_wrapper div class=s_form div class=s_form_wrapper div id=lg img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129 /div form id=form name=f action