python3 requests 库学习

#requests
#http 请求
import requests
url='http://www.baidu.com'
requests.post('url')
requests.get('url')
requests.put('url')
requests.head('url')
requests.options('url')

#get请求
response=requests.get('url')
#参数请求
import requests
data={
    'name':'jmc',
    'age':'1'
}
response=requests.get('url',params=data)
print (response.text)

#解析json
import requests
response=requests.get('url')
print (response.json())

#获取二进制
import requests
response=requests.get('url')
print (response.content)
#图片获取
import requests
response=response.get('url')
with open('c:1.jpg','wb') as f:
    f.write(response.content)
    f.close()
#添加headers
import requests
headers={
    'User-Agent':'jmc'
    'jmc':'jmc'
}
response=requests.get('url',headers=headers)

#post
import requests
data={
    'name':'g0mini'
    'age':'1'
}
headers={
    'User-Agent':'jmc'
}
response=requests.post('url',data=data,headers=headers)


#response
response.status_code
response.headers
response.cookie
response.url
response.history

#文件上传
import requests
files={
    'file':open('c:/1.text','rb')
}
response=requests.post('url',files=files)
#设置文件名,文件类型和请求头
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)



#获取cookie
import requests
conn=requests.cookie
url=''
response=requests.get('url','conn')

#证书验证
import requests
response=requests.get('url',verify=False)


#代理设置
import requests
proxies={
    'http':'http://127.0.0.1:8080'
    'https':'https://127.0.0.1:8080'
}
response=requests.get('url',proxies=proxies)


#认证代理
import requests
response=requests.get('http://127.0.0.1'auth=('user','123'))
#简单登录
import requests
for i in range(1,3):
    print (i)
    data={
        'name':'1 %d'%i,
    }
    response=requests.post('url',data=data)

 

posted @ 2019-04-04 14:21  G0mini  阅读(231)  评论(0编辑  收藏  举报