# -*- coding:utf-8 -*- # requests 网络请求包,基于urllib封装第三方请求包 # pip install requesrs 下载包 import requests # 支持所有类型的请求 # requests.get() # requests.post() # requests.delete() # requests.put() # requests.head() # requests.options() ''' # 发送请求时可以传递的参数 def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None) ''' proxies = { 'http':'120.92.119.187:10000' } response = requests.get('http://kaoshi.zhiyou900.com:8888/edustu/login/login.spr',proxies = proxies) # 返回数据的bytes类型数据 # print(response.content) # 返回数据的str类型数据 # print(response.text) # 状态码 print(response.status_code) # headers字典 print(response.headers) print(response.cookies) print(response.request) print(response.url) print('================================') # 访问的历史记录,如果服务器没有返回,是一个空列表 print(response.history) # 编码格式 print(response.encoding) # 原因短语 print(response.reason) # 此次请求消耗的时间 print(response.elapsed) url = 'http://kaoshi.zhiyou900.com:8888/edustu/login/login.spr' # requests中使用代理 # proxies = { # 'http':'122.114.31.177:808' # } # 需要上传参数 data = { 'j_username':'15896901897', 'j_password':'123456' } # 发送post请求 response = requests.post(url,data=data,headers={},proxies=proxies) with open('1.html','w') as f: f.write(response.text) # 获取json数据 response =requests.get('http://api.map.baidu.com/telematics/v3/weather?location=郑州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?') import json # 转换为python # json.loads(response.text) # json() 针对返回的json数据,可以直接使用json()将其转换python对象 rs = response.json() print(type(rs))
基于python的-requests模块
最新推荐文章于 2023-09-06 00:49:55 发布