requests基本使用

redis开启

  • 开启服务端,在cmd中输入redis-server
  • 开启客户端,在cmd中输入redis-cli

mongodb开启

  • 开启服务端,cmd中输入mongod
  • 开启客户端,cmd中输入mong.exe

requests的参数

import requests

data = {
    'name':'perfey',
    'age':'18'
}
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    'cookie': 'd_c0="AIAkb1Qu3w2PTvKo0YXcKkNxgdk5LL_dzFI=|1531095721"; _zap=88a080ea-b5eb-49fd-bdbc-84f6b0d81cff; _xsrf=y5QFxGkZVe1KU2BchUd32iN3fpWyhCEN'
}
proxies = {
    'http':'http://10.10.1.10:3128',
    'https':'http://10.10.1.10:1080'
}
response = requests.get('www.baidu.com',params=data,headers=headers,verify=False,proxies=proxies,timeout=1,auth=('username','password'))  # params是参数,相当于访问的是www.baidu.com?name=perfey&age=18 verify=False设置不进行证书验证 proxies设置代理ip timeout超时设置,超时抛出异常 auth身份认证,对于一些需要用户名和密码的网站需要使用
print(response.text)  #字符串
print(response.content) #字节
print(response.json()) #json格式的响应
print(response.status_code) #获取响应状态
print(response.headers)  #获取响应头
print(response.cookies) #获取cookies

# post请求
request.post('www.baidu.com',data=data)  # 这里的data是请求体的内容

files = {'file':open('faicon.ico','rb')}
request.post('http://httpbin.org/post',files=files) # 上传文件

# 会话维持,自动保存cookies并使用这个cookies
import requests
s = request.Session()
s.get('http://httpbin.org/cookies/set/number/1234556789')
r = s.get('http://httpbin.org/cookies')
print(t.text)
原文地址:https://www.cnblogs.com/liurenli/p/10765259.html