判断请求是否成功
assert response.status_code == 200
url 编码
https://www.baidu.com/s?wd=%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80
字符串格式化的另一种方式
"学习{}编程语言".format("python")
requests模块发送带headers的请求和带参数的请求
发送简单的请求
需求:通过requests向百度首页发送请求,获取百度首页的数据
response = requests.get(url)
response的常用方法:
- response.text
- response.content
- response.status_code
- response.request.headers
- response.headers
In [1]: import requests
In [2]: response = requests.get("http://www.baidu.com")
In [3]: response.status_code
Out[3]: 200
In [4]: assert response.status_code == 200
In [5]: assert response.status_code == 300
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-5-245507ec34d1> in <module>
----> 1 assert response.status_code == 300
AssertionError:
In [6]: response.headers
Out[6]: {
'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform'