请求
基本GET请求
# 导入Requests模块
import requests
response = requests.get("https://www.baidu.com")
# 现在我们就有了一个名为response的Response对象。我们可以从这个对象中获取所有我们想要的信息
# 用text获取响应内容
print(response.text)
带参数的GET请求
# 直接把参数加到到网址后面,用?分隔,用&拼接
response = requests.get("https://www.baidu.com?name=Bob&age=22")
# 以字典形式传值
data = {
'name':'Bob',
'age': 22
}
response = requests.get("https://www.baidu.com", params = data)
print(response.text)
解析Json
import json
# 当返回结果为json格式的字符串时,可以直接调用json()方法将其转化为json对象
response.json()
json.loads(response.text) # 可以得到相同的结果
获取二进制数据
response.content
添加headers
import requests
headers = {
"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
response = requests.get("https://www.zhihu.com", headers=headers)
print(response.text)
基本POST请求
import requests
data = {
'name': "Bob",
'age': 22
}
# 数据字典在发出请求时会自动编码为表单形式
response = requests.post("https://www.zhihu.com", data=data)
print(response.text)
响应
response属性
import requests
response = requests.get('http://www.jianshu.com')
# 状态码
response.status_code
# cookies
response.cookies
# headers
response.headers
# url
response.url
# history
response.history
状态码判断
exit() if not response.status_code == requests.codes.ok else print("Request Successfuly!")
exit() if not response.status_code == 200 else print("Request Successfuly!")
高级操作
文件上传
import requests
files = {'file': open('favicon.ico', 'rb')}
response = requests.post("http://httpbin.org/post", files=files, timeout=1)
print(response.text)
获取cookies
import requests
response = requests.get("https://www.baidu.com")
print(response.cookies)
for key,value in response.cookies.items():
print(key + '=' + value)
会话维持
import requests
requests.get("http://httpbin.org/cookies/set/number/123456789")
response = requests.get("http://httpbin.org/cookies")
print(response.text)
# cookies为空是因为发起了两次请求,而这两个请求是两个独立的过程,相当于用一个浏览器设置了cookie,然后用另一个浏览器获取cookies这个页面,即在
# 两个互不关联的浏览器中进行操作
# 如何在同一个浏览器中实现set和get?
# 设置Session对象,再发起两次请求
s = requests.Session()
s.get("http://httpbin.org/cookies/set/number/123456789")
response = s.get("http://httpbin.org/cookies")
print(response.text)
代理设置
import requests
proxies = {
# 添加一些代理IP
"http": "http://127.0.0.1:9743",
"https": "https://127.0.0.1:9743"
}
# 如果代理是有用户名和密码的
proxies = {
# 添加一些代理IP, 传入用户名和密码信息
"http": "http://user:password@127.0.0.1:9743/",
}
response = requests.get("https://www.taobao.com", proxies=proxies)
print(response.status_code)
超时设置和异常处理
import requests
from requests.exceptions import ReadTimeout, ConnectTimeout
try:
response = requests.get("http://httpbin.org/get", timeout=0.1)
print(response.status_code)
except ReadTimeout:
print("ReadTimeout")
except ConnectTimeout:
print("ConnectTimeout")