爬虫入门之爬虫原理以及请求响应

爬虫入门之爬虫原理以及请求响应

爬虫需要用到的库, 叫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 requests

get请求(获取百度网站的响应):

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> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值