1.4requests的简单使用

问题:为什么要学习requests,而不是urllib?
1、requests的底层实现就是urllib
2、requests在python2 和python3中通用,方法完全一样
3、requests简单易用
4、requests能够自动帮助我们解压(gzip压缩的等)网页内容

Requests: 让 HTTP 服务人类

虽然Python的标准库中 urllib 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “HTTP for Humans”,说明使用更简洁方便。

Requests 继承了urllib的所有特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。

requests 的底层实现其实就是 urllib

Requests的文档非常完备,中文文档也相当不错。Requests能完全满足当前网络的需求,支持Python 2.6–3.5,而且能在PyPy下完美运行。

开源地址:https://github.com/kennethreitz/requests
中文文档 API: http://docs.python-requests.org/zh_CN/latest/index.html

安装方式

利用 pip 安装 或者利用 easy_install 都可以完成安装:

$ pip install requests

$ easy_install requests

requests的作用

作用:发送网络请求,返回响应数据

中文文档 API: http://docs.python-requests.org/zh_CN/latest/index.html

需要解决的问题:如何使用requests来发送网络请求?

发送简单的请求

需求:通过requests向百度首页发送请求,获取百度首页的数据

response = requests.get(url)

在这里插入图片描述

response = requests.get("http://www.baidu.com/")
# 也可以这么写
# response = requests.request("get", "http://www.baidu.com/")

response的常用方法:

response.text 
respones.content
response.status_code
response.request.headers
response.headers

响应头
在这里插入图片描述请求头
在这里插入图片描述
User-Agent:此处是默认的,后期使用默认将会被识别出来,爬虫失败

状态码:
在这里插入图片描述
响应内容:
在这里插入图片描述
请求内容:
在这里插入图片描述返回的内容其实不多,这是因为识别出不是正规的浏览器请求百度

response.text 和response.content的区别

response.text
类型:str
解码类型: 根据HTTP 头部对响应的编码作出有根据的推测,推测的文本编码
如何修改编码方式:response.encoding=”gbk”

response.content
类型:bytes
解码类型: 没有指定
如何修改编码方式:response.content.deocde(“utf8”)

更推荐使用response.content.deocde()的方式获取响应的html页面

发送带header的请求

为什么请求需要带上header?
模拟浏览器,欺骗服务器,获取和浏览器一致的内容

header的形式:字典

headers = {“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36”}
用法: requests.get(url,headers=headers)
在这里插入图片描述

发送带参数的请求

什么叫做请求参数:
列1: http://www.webkaka.com/tutorial/server/2015/021013/ ☓
例2: https://www.baidu.com/s?wd=python&c=b

参数的形式:字典

kw = {‘wd’:‘长城’}

用法:requests.get(url,params=kw)

#coding:utf-8
#file: __init__.py.py
#@author: young
#@contact: [email protected]
#@time: 2019/12/22 14:24
import  requests
url_temp = "http://www.baidu.com/s?"
#有无?均可以
p = {"wd  ": "传智播客"}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
r = requests.get(url_temp,headers= headers ,params= p)
print(r.status_code)
print(r.request.url)
#需要解码,解码后内容为传智播客

在这里插入图片描述需要取出参数:

#coding:utf-8
#file: __init__.py.py
#@author: young
#@contact: [email protected]
#@time: 2019/12/22 14:24
import  requests
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
url = "https://www.baidu.com/?wd++=%E4%BC%A0%E6%99%BA%E6%92%AD%E5%AE%A2"
r = requests.get(url,headers= headers )
print(r.status_code)
print(r.request.url)

在这里插入图片描述

小栗子

通过requests获取新浪首页

#coding:utf-8
#file: sina.py
#@author: young
#@contact: [email protected]
#@time: 2019/12/22 15:39

import  requests
response = requests.get("http://www.sina.com")
print(response.request.headers)
print(response.content.decode())

结果:

{'User-Agent': 'python-requests/2.12.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<!DOCTYPE html>
<!-- [ published at 2017-06-09 15:15:23 ] -->
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>新浪首页</title>
    <meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
  ...
import  requests
response = requests.get("http://www.sina.com")
print(response.request.headers)
print(response.text)
{'User-Agent': 'python-requests/2.12.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
<!DOCTYPE html>
<!-- [ published at 2017-06-09 15:18:10 ] -->
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>新浪首页</title>
    <meta name="keywords" content="新浪,新浪网,SINA,sina,sina.com.cn,新浪首页,门户,资讯" />
    <meta name="description" content="新浪网为全球用户24小时提供全面及时的中文资讯,内容覆盖国内外突发新闻事件、体坛赛事、娱乐时尚、产业资讯、实用信息等,设有新闻、体育、娱乐、财经、科技、房产、汽车等30多个内容频道,同时开设博客、视频、论坛等自由互动交流空间。" />
    <link rel="mask-icon" sizes="any" href="//www.sina.com.cn/favicon.svg" color="red">
`

产生问题的原因分析

1、 requests默认自带的Accept-Encoding导致或者新浪默认发送的就是压缩之后的网页,
2、但是为什么content.read()没有问题,因为requests,自带解压压缩网页的功能
3、当收到一个响应时,Requests 会猜测响应的编码方式,用于在你调用response.text 方法时对响应进行解码。Requests 首先在 HTTP 头部检测是否存在指定的编码方式,如果不存在,则会使用 chardet.detect来尝试猜测编码方式(存在误差)
4、更推荐使用response.content.deocde()

发布了60 篇原创文章 · 获赞 8 · 访问量 3306

猜你喜欢

转载自blog.csdn.net/qq_43476433/article/details/103652774