Python爬虫利器一之Requests库的用法

2023-11-10 23:48

本文主要是介绍Python爬虫利器一之Requests库的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助。入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取。那么这一节来简单介绍一下 requests 库的基本用法

安装

利用 pip 安装

pip3 install requests

或者利用 easy_install

easy_install requests

通过以上两种方法均可以完成安装。

引入

首先我们引入一个小例子来感受一下

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('http://www.baidu.com')
print('type(request)', type(request))
print('request.status_code', request.status_code)
print('request.encoding', request.encoding)
print('request.cookies', request.cookies)
print('request.text', request.text)

以上代码我们请求了本站点的网址,然后打印出了返回结果的类型,状态码,编码方式,Cookies等内容。

运行结果如下

type(request) <class 'requests.models.Response'>
request.status_code 200
request.encoding ISO-8859-1
request.cookies <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
request.text <!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=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

基本请求

requests库提供了http所有的基本请求方式。例如

request = requests.get(url)
request = requests.post(url)
request = requests.put(url)
request = requests.delete(url)
request = requests.head(url)
request = requests.opinions(url)

GET请求

基本GET请求

最基本的GET请求可以直接用get方法

#!/usr/bin/env python3
#coding:utf-8import requestsparams = {'key1': 'value1','key2': 'value2'
}
request = requests.get('http://httpbin.org/get', params=params)
print(request.url)

运行结果

http://httpbin.org/get?key1=value1&key2=value2

GET JSON

我们能读取服务器响应的内容。以 GitHub 时间线为例:

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('https://api.github.com/events')
print('request.text', request.text)
print('request.json()', request.json)

输出

request.text [{"id":"5435287313","type":"PushEvent","actor":=...}]
request.json() <bound method Response.json of <Response [200]>>

GET原始套接字内容

如果想获取来自服务器的原始套接字响应,可以取得 r.raw 。 不过需要在初始请求中设置 stream=True

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('https://api.github.com/events', stream=True)
print('request.raw', request.raw)
print('request.raw.read(10)', request.raw.read(10))

这样就获取了网页原始套接字内容

request.raw <requests.packages.urllib3.response.HTTPResponse object at 0x106b5f9b0>
request.raw.read(10) b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

添加headers

通过传递headers参数来添加headers

#!/usr/bin/env python3
#coding:utf-8import requestsparams = {'key1': 'value1','key2': 'value2'
}
headers= {'content-type': 'application/json'
}
request = requests.get('http://httpbin.org/get', params=params, headers=headers)
print(request.url)

输出

http://httpbin.org/get?key1=value1&key2=value2

POST请求

基本POST请求

对于 POST 请求来说,我们一般需要为它增加一些参数。那么最基本的传参方法可以利用data 这个参数

#!/usr/bin/env python3
#coding:utf-8import requestsdata = {'key1': 'value1','key2': 'value2'
}
request = requests.post('http://httpbin.org/post', data=data)
print('request.text', request.text)

输出结果

request.text {"args": {},"data": "","files": {},"form": {"key1": "value1","key2": "value2"},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Content-Length": "23","Content-Type": "application/x-www-form-urlencoded","Host": "httpbin.org","User-Agent": "python-requests/2.13.0"},"json": null,"origin": "120.236.174.172","url": "http://httpbin.org/post"
}

POST JSON

有时候我们需要传送的信息不是表单形式的,需要我们传JSON格式的数据过去,所以我们可以用json.dumps()方法把表单数据序列化

#!/usr/bin/env python3
#coding:utf-8import json
import requestsdata = {'some': 'data'
}
request = requests.post('http://httpbin.org/post', data=json.dumps(data))
print('request.text', request.text)

输出

request.text {"args": {},"data": "{\"some\": \"data\"}","files": {},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Content-Length": "16","Host": "httpbin.org","User-Agent": "python-requests/2.13.0"},"json": {"some": "data"},"origin": "120.236.174.172","url": "http://httpbin.org/post"
}

在2.4.2版本后,直接使用json参数就可以进行编码

#!/usr/bin/env python3
#coding:utf-8import json
import requestsdata = {'some': 'data'
}
# request = requests.post('http://httpbin.org/post', data=json.dumps(data))
request = requests.post('http://httpbin.org/post', json=data)
print('request.text', request.text)

上传文件

如果想要上传文件,那么直接用files参数即可

#!/usr/bin/env python3
#coding:utf-8import requestsfiles = {'file': open('test.txt', 'rb')
}
request = requests.post('http://httpbin.org/post', files=files)
print('request.text', request.text)

输出

request.text {"args": {},"data": "","files": {"file": "hello word!\n"},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Content-Length": "156","Content-Type": "multipart/form-data; boundary=e76e934f387f4013a0cb03f0cc7f636d","Host": "httpbin.org","User-Agent": "python-requests/2.13.0"},"json": null,"origin": "120.236.174.172","url": "http://httpbin.org/post"
}

上传流

requests 是支持流式上传的,这允许你发送大的数据流或文件而无需先把它们读入内存。要使用流式上传,仅需为你的请求体提供一个类文件对象即可

#!/usr/bin/env python3
#coding:utf-8import requestswith open('test.txt', 'rb') as f:request = requests.post('http://httpbin.org/post', data=f)
print('request.text', request.text)

输出结果和直接用文件上传一样

Cookies

如果一个响应中包含了cookie,那么我们可以利用 cookies 变量来拿到

也可以利用cookies参数来向服务器发送cookies信息

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('http://httpbin.org/cookies')
print('request.cookies', requests.cookies)
print('request.text', request.text)
cookies = {'cookies_are': 'working'
}
request = requests.get('http://httpbin.org/cookies', cookies=cookies)
print('request.cookies', requests.cookies)
print('request.text', request.text)

输出

request.cookies <module 'requests.cookies' from '/usr/local/lib/python3.6/site-packages/requests/cookies.py'>
request.text {"cookies": {}
}request.cookies <module 'requests.cookies' from '/usr/local/lib/python3.6/site-packages/requests/cookies.py'>
request.text {"cookies": {"cookies_are": "working"}
}

超时配置

可以利用 timeout 变量来配置最大请求时间

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('http://www.google.com.hk', timeout=0.01)
print(request.url)

如果超时,会抛出一个异常

Traceback (most recent call last):File "/usr/local/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 141, in _new_conn(self.host, self.port), self.timeout, **extra_kw)File "/usr/local/lib/python3.6/site-packages/requests/packages/urllib3/util/connection.py", line 83, in create_connectionraise errFile "/usr/local/lib/python3.6/site-packages/requests/packages/urllib3/util/connection.py", line 73, in create_connectionsock.connect(sa)
socket.timeout: timed out

会话对象

在以上的请求中,每次请求其实都相当于发起了一个新的请求。也就是相当于我们每个请求都用了不同的浏览器单独打开的效果。也就是它并不是指的一个会话,即使请求的是同一个网址。比如

#!/usr/bin/env python3
#coding:utf-8import requestsrequests.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
request = requests.get('http://httpbin.org/cookies')
print(request.text)

输出

{"cookies": {}
}

很明显,这不在一个会话中,无法获取 cookies,那么在一些站点中,我们需要保持一个持久的会话怎么办呢?就像用一个浏览器逛淘宝一样,在不同的选项卡之间跳转,这样其实就是通过request.Session建立了一个长久会话

解决方案如下

#!/usr/bin/env python3
#coding:utf-8import requests# 通过session建立长久会话
session = requests.Session()
session.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
request = session.get('http://httpbin.org/cookies')
print(request.text)

输出

{"cookies": {"sessioncookie": "123456789"}
}

发现可以成功获取到 cookies 了,这就是建立一个会话到作用。体会一下。

那么既然会话是一个全局的变量,那么我们肯定可以用来全局的配置了。

当在会话中设置headers,又在请求中设置headers,两个变量都会传送过去

#!/usr/bin/env python3
#coding:utf-8import requestsheaders1 = {'test1': 'true'
}
headers2 = {'test2': 'true'
}
session = requests.Session()
session.headers.update(headers1)
request = session.get('http://httpbin.org/headers', headers=headers2)
print(request.text)

输出

{"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Host": "httpbin.org","Test1": "true","Test2": "true","User-Agent": "python-requests/2.13.0"}
}

SSL证书验证

现在随处可见 https 开头的网站,Requests可以为HTTPS请求验证SSL证书,就像web浏览器一样。要想检查某个主机的SSL证书,你可以使用 verify参数(默认为True)

现在 12306 证书不是无效的嘛,来测试一下

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('https://kyfw.12306.cn/otn', verify=True)
print(request.text)

输出

Traceback (most recent call last):File "/usr/local/lib/python3.6/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 436, in wrap_socketcnx.do_handshake()File "/usr/local/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1426, in do_handshakeself._raise_ssl_error(self._ssl, result)File "/usr/local/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1174, in _raise_ssl_error_raise_current_error()File "/usr/local/lib/python3.6/site-packages/OpenSSL/_util.py", line 48, in exception_from_error_queueraise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')]

如果想跳过证书验证,只需要把verify设置为False

来试下 github 的

#!/usr/bin/env python3
#coding:utf-8import requestsrequest = requests.get('https://kyfw.12306.cn/otn', verify=True)
print(request.text)

请求正常

代理

如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求

#!/usr/bin/env python3
#coding:utf-8import requestsproxies = {"https://www.google.com.hk/": "192.168.199.101"
}
request = requests.get('https://www.google.com.hk/', proxies=proxies)
print(request.text)

API

以上讲解了 requests 中最常用的参数,如果需要用到更多,请参考官方文档 API

API

结语

以上总结了一下 requests 的基本用法,如果你对爬虫有了一定的基础,那么肯定可以很快上手,在此就不多赘述了。

练习才是王道,大家尽快投注于实践中吧

转载于:静觅 » Python爬虫利器一之Requests库的用法

这篇关于Python爬虫利器一之Requests库的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/386088

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及