Requests库的基本使用

  1.基本写法

import requests
response = requests.get('https://httpbin.org/get')
print(response.text)

  2.带参数GET请求

import requests
response = requests.get('https://httpbin.org/get?name=getmey&age=22')
print(response.text)

 

import requests
data={
    'name':'getmey',
    'age':22
}
response = requests.get('https://httpbin.org/get',params=data)
print(response.text)

  3.解析JSON

import requests
import json
response=requests.get('https://httpbin.org/get')
print(type(response.text))
print(response.json())
print(json.loads(response.text))
print(type(response.json()))

  4.获取二进制数据(这里以获取图片的二进制数据为例)

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('C:/Users/THINK/Desktop/python/favicon.ico','wb') as f:
    f.write(response.content)
    f.close()

  5.添加headers

import requests
response=requests.get('https://www.zhihu.com/explore')
print(response.text)
import requests
headers={
    'User-Agent':'Mozilla/5.0(Macintosh;Intel Mac OS X 10_11_4)APPleWebKit/537.36(KHTML,like Gecko)Chrome/52.0.2743.116 Safari/537.36'
}
response=requests.get('https://www.zhihu.com/explore',headers=headers)
print(response.text)

  6.基本POST请求

import requests
data={'name':'germey','age':'22'}
response = requests.post('https://httpbin.org/post',data=data)
print(response.text)
import requests
data={'name':'germey','age':'22'}
headers={
    'User-Agent':'Mozilla/5.0(Macintosh;Intel Mac OS X 10_11_4)APPleWebKit/537.36(KHTML,like Gecko)Chrome/52.0.2743.116 Safari/537.36'
}
response = requests.post('https://httpbin.org/post',data=data,headers=headers)
print(response.text)
import requests
response=requests.get('https://www.jianshu.com')
print(type(response.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)

  7.状态码判断

import requests
response = requests.get('http://www.jianshu.com/hello.html')
if not response.status_code == 404 exit()
    else print('Successfully')

  8.文件上传

import requests
files = {'file':open('C:/Users/THINK/Desktop/python/favicon.ico','rb')}
response = requests.post('https://httpbin.org/post',files=files)
print(response.text)

  9.获取Cookie

import requests
response = requests.get('https://www.baidu.com')
print(response.cookies)
for key,value in response.cookies.items():
    print(key+'='+value)

  10.会话维持

import requests
requests.get('http://httpbin.org/cookies/set/number/123456789')
response = requests.get('http://httpbin.org/cookies')
print(response.text)
import requests
s=requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
//第一次运行和第二次结果不一样,因为第一次相当于请求了两次,会话没有保持,第二次创建了session对象,是得会话得以保持

  11.证书验证

import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)

  

import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
import requests
response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
print(response.status_code)

  12.认证设置

import requests
from requests.auth import HTTPBasicAuth
r = requests.get('http://120.27.34.24:9001',auth=HTTPBasicAuth('user','123'))
print(r.status_code)

  13.超时设置

import requests
from requests.exceptions import ConnectTimeout
try:
    response = requests.get('https://httpbin.org/get',timeout = 0.1)
except ConnectTimeout:
    print('Timeout')
import requests
r = requests.get('http://120.27.34.24:9001',auth=HTTPBasicAuth('user','123'))
print(r.status_code)

  14.异常处理

import requests
from requests.exceptions import ConnectTimeout,ReadTimeout,HTTPError,RequestException
try:
    response = requests.get('https://httpbin.org/get',timeout = 0.1)
except ConnectTimeout:
    print('ConnectTimeout')
except ReadTimeout:
    print('Timeout')
except HTTPError:
    print('HTTPError')
except RequestException:
    print('RequestException')

希望对大家能有所帮助。

posted @ 2018-09-11 17:17  大神笨蛋  阅读(305)  评论(0编辑  收藏  举报