pip3 install requests
验证完成安装 命令行下 import requests 无报错信息即安装成功
一般常用的测试网页为 http://httpbin.org/get
import requests response = requests.get('http://www.baidu.com') print(type(response)) print(response.status_code) print(type(response.text)) print(response.text) #网页信息 print(response.cookies)
get请求
import requests response = requests.get('http://httpbin.org/get') #测试网页 print(response.text)
带参数的get请求
#通过params参数构建url地址
#params前面是逗号 import requests data = { 'name':'liu', 'age':22 } response = requests.get('http://httpbin.org/get',params=data) print(response.text) 打印结果 { "args": { "age": "22", "name": "liu" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "User-Agent": "python-requests/2.20.0" }, "origin": "210.77.180.38", "url": "http://httpbin.org/get?name=liu&age=22" }
解析json
import requests
import json
response = requests.get('http://httpbin.org/get')
print(response.text)
print(response.json())
print(json.loads(response.text)) #两次返回结果是一样的
print(type(response.json()))
打印结果
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.20.0"
},
"origin": "210.77.180.38",
"url": "http://httpbin.org/get"
}
{'headers': {'User-Agent': 'python-requests/2.20.0', 'Connection': 'close', 'Accept': '*/*', 'Host': 'httpbin.org', 'Accept-Encoding': 'gzip, deflate'}, 'origin': '210.77.180.38', 'args': {}, 'url': 'http://httpbin.org/get'}
{'headers': {'User-Agent': 'python-requests/2.20.0', 'Connection': 'close', 'Accept': '*/*', 'Host': 'httpbin.org', 'Accept-Encoding': 'gzip, deflate'}, 'origin': '210.77.180.38', 'args': {}, 'url': 'http://httpbin.org/get'}
<class 'dict'>
获取二进制数据
import requests response = requests.get('http://github.com/favicon.ico') print(type(response.text),type(response.content)) print(response.text) print(response.content)
打印结果
下载图片
import requests response = requests.get('http://github.com/favicon.ico') with open('favicon.ico','wb') as f: f.write(response.content) #content 获取二进制数据 f.close() 运行后可在路径下找到下载的图片
#以访问知乎为例
import requests
response = requests.get('https://www.zhihu.com/explore')
print(response.text)
打印结果
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>openresty</center>
</body>
</html>
通过加headers来访问
import requests headers = { 'User-Agent':..................自行添加..................... } response = requests.get('https://www.zhihu.com/explore',headers=headers) print(response.text)
基本post请求
import requests data = { 'name':'liu', 'age':22 } response = requests.post('http://httpbin.org/post',data = data) print(response.text) 打印结果 { "args": {}, "data": "", "files": {}, "form": { "age": "22", "name": "liu" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "15", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.20.0" }, "json": null, "origin": "210.77.180.38", "url": "http://httpbin.org/post" }
添加headers 与get方法一样
import requests data = { 'name':'liu', 'age':22 } headers = {'User-Agent':.............} response = requests.post('http://httpbin.org/post',data = data,headers=headers) print(response.json())
响应
response 属性
import requests response = requests.get('http://www.baidu.com') print(type(resopnse.status_code),response.status_code) print(type(response.headers),response.headers) print(type(response.cookies),response.cookies) print(type(response.url),response.url) print(type(response.history),response.history) 打印结果 <class 'int'> 200 <class 'requests.structures.CaseInsensitiveDict'> {'Content-Type': 'text/html', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Transfer-Encoding': 'chunked', 'Server': 'bfe/1.0.8.18', 'Content-Encoding': 'gzip', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:36 GMT', 'Date': 'Thu, 08 Nov 2018 07:18:47 GMT', 'Pragma': 'no-cache', 'Connection': 'Keep-Alive'} <class 'requests.cookies.RequestsCookieJar'> <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]> <class 'str'> http://www.baidu.com/ <class 'list'> []
状态码判断
import requests response = requests.get('http://www.baidu.com') exit() if not response.status_code == requests.codes.ok else print('访问成功') exit() if not response.status_code ==200 else print('访问成功') #可以直接用状态码200替换 打印结果 访问成功 访问成功