GET 请求
>>> r = requests.get("https://httpbin.org/ip")
>>> r
<Response [200]> # 响应对象
>>> r.status_code # 响应状态码
200
>>> r.content # 响应内容
'{\n "origin": "183.237.232.123"\n}\n'...
POST 请求
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})
自定义请求头
这个经常会用到,服务器反爬虫机制会判断客户端请求头中的User-Agent是否来源于真实浏览器,所以,我们使用Requests经常会指定UA伪装成浏览器发起请求
>>> url = 'https://httpbin.org/headers'
>>> headers = {'user-agent': 'Mozilla/5.0'}
>>> r = requests.get(url, headers=headers)
参数传递
很多时候URL后面会有一串很长的参数,为了提高可读性,requests 支持将参数抽离出来作为方法的参数(params)传递过去,而无需附在 URL 后面,例如请求 url http://bin.org/get?key=val ,可使用
>>> url = "http://httpbin.org/get"
>>> r = requests.get(url, params={"key":"val"})
>>> r.url
u'http://httpbin.org/get?key=val'...
指定Cookie
Cookie 是web浏览器登录网站的凭证,虽然 Cookie 也是请求头的一部分,我们可以从中剥离出来,使用 Cookie 参数指定
>>> s = requests.get('http://httpbin.org/cookies', cookies={'from-my': 'browser'})
>>> s.text
u'{\n "cookies": {\n "from-my": "browser"\n }\n}\n'...
设置超时
当发起一个请求遇到服务器响应非常缓慢而你又不希望等待太久时,可以指定 timeout 来设置请求超时时间,单位是秒,超过该时间还没有连接服务器成功时,请求将强行终止。
r = requests.get('https://google.com', timeout=5)...
设置代理
一段时间内发送的请求太多容易被服务器判定为爬虫,所以很多时候我们使用代理IP来伪装客户端的真实IP。
import requests
proxies = {
'http': 'http://127.0.0.1:1080',
'https': 'http://127.0.0.1:1080',
}...
r = requests.get('http://www.kuaidaili.com/free/', proxies=proxies, timeout=2)
Session
如果想和服务器一直保持登录(会话)状态,而不必每次都指定 cookies,那么可以使用 session,Session 提供的API和 requests 是一样的。
import requests...
s = requests.Session()
s.cookies = requests.utils.cookiejar_from_dict({"a": "c"})
r = s.get('http://httpbin.org/cookies')
print(r.text)
# '{"cookies": {"a": "c"}}'
r = s.get('http://httpbin.org/cookies')
print(r.text)
# '{"cookies": {"a": "c"}}'...
分享到:
相关推荐
A fast-paced guide that demonstrates the use of Python Requests with the help of examples Learn web scraping with Beautiful Soup and Python Requests libraries Interact with social networking sites ...
python requests模块的安装包,以及requests所需的依赖包certifi、chardet等等
Learn how to integrate your applications seamlessly with web services using Python Requests About This Book A fast-paced guide that demonstrates the use of Python Requests with the help of examples ...
python-requests离线包,没用网络也可以使用
Python Requests 2.10.0 文档,自己排版了一下
pythonrequests快速入门pythonrequests快速入门pythonrequests快速入门pythonrequests快速入门
学习Python requests(丢掉urllib2吧)最好最新的书,含pdf, epub两个格式,电脑手机上同时学习用
主要介绍了构建高效的python requests长连接池详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
python requests官方中文文档,进阶用法。本文档覆盖了requests库的一些高级特性
Python Requests Essentials 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或...
Python Requests Essentials高清带书签文字版 这个是带完整目录书签的高清pdf文字版本,内容可以复制的哦
python requests re 正则 爬虫 精简 万能,理论上可以下载任何网页的图片,可以试试看,简单易懂
python requests接口自动化把登录token保存在文件,实现所有用例登录一次(csdn)————程序.pd
Requests 是一个易于使用的golang HTTP库,与Python requests类似
python requests 包 用于http 的请求第三方包
Python requests 是一个常用的 HTTP 请求库,可以方便地向网站发送 HTTP 请求,并获取响应结果。Python requests爬虫实例
khttp是Kotlin中HTTP请求的简单库。 它的功能类似于Python的requests模块。
python3的 requests离线安装包,仅window环境能用,离线安装方法如下: 解压到任意目录后,cd到requests目录 然后 pip install --no-index --find-links=pack -r requirements.txt 或者 python -m pip install --no-...
网络请求不可避免会遇上请求超时的情况,在 requests 中,如果不设置你的程序可能会永远失去响应。 超时又可分为连接超时和读取超时。 连接超时 连接超时指的是在你的客户端实现到远端机器端口的连接时(对应的是 ...
python+requests+pytest+allure+yaml 一、数据类型处理 “${read_extract_data(tag_id)}” 替换成 110 二、DDT数据驱动封装 数据驱动可以利用 excel,csv,yaml 用excel,csv的缺点 1.数据类型难处理 2.调用函数不是...