Requests库的使用

基于urllib改写的库

示例:

import requests

response=requests.get('http://www.baidu.com')#get请求
print(response.status_code,response.url,response.cookies,response.text,sep='\n')
import requests
response=requests.post('http://httpbin.org/post')#post请求
print(response.text)#.text得到的都是字符串类型的值

带参数的get请求

import requests
data={
    'name':'abc',
    'age':15
}
response=requests.get('http://httpbin.org/get',params=data)#post的参数请求时data=data
print(response.text)

----------------------------------------------
#或者直接将参数拼接在url上
import requests response=requests.get('http://httpbin.org/get?name=adas&age=12') print(response.text)

将返回的结果变为json格式

import requests
import json
response=requests.get('http://httpbin.org/get')
print(response.json())
print(json.loads(response.text))#等价于上面的

获取二进制数据

1 import requests
2 response=requests.get('https://weibo.com/favicon.ico')
3 print(response.content)
4 with open('weibo.ico','wb')as f:
5     f.write(response.content)

View Code