标签:获取 option let jar params code 请求 text options
1、Requests 用法
例子:
import requests
response = requests.get(‘https://www.baidu.com‘)
print(type(response))
print(response.status_code) //200
print(type(response.text))
print(response.text)
print(response.cookies) //不需要声明cookiejar
各种请求方式
import requests
requests.post(‘http://httpbin.org/post‘)
requests.put(‘http://httpbin.org/post‘)
requests.delete(‘http://httpbin.org/post‘)
requests.options(‘http://httpbin.org/post‘)
基本Get请求
import requests
response =requests.get(‘http://httpbin.org/get‘)
print(response.text)
带参数GET请求
import requests
response =requests.get(‘http://httpbin.org/get?name=germey&age=22‘)
print(response.text)
或者
data={
‘name‘:‘germey‘,
‘age‘:22
}
response =requests.get(‘http://httpbin.org/get‘,params=data)
print(response.text)
解析Json 方法
import requests
response =requests.get(‘http://httpbin.org/get‘)
print(type(response.text))
print(response.json()) //json
或
print(json.loads(response.text))
print(type(response.json()))
获取二进制数据
import requests
response=requests.get(‘https://github.com/favicon.ico‘)
print(type(response.text),type(response.content))
print(response.text)
print(response.content)
保存图片
import requests
response=requests.get(‘https://github.com/favicon.ico‘)
with open(‘favicon.icon‘,‘wb‘) as f:
f.write(response.content)
f.close()
添加headers
import requests
response= requests.get(‘https://www.zhihu.com/expiore‘)
print(response.text)
标签:获取 option let jar params code 请求 text options
原文地址:http://www.cnblogs.com/youmingkuang/p/7786778.html