python调用api实例讲解
作者:小妮浅浅 时间:2023-09-16 02:32:33
我们在做自动化运维的时候,经常需要调用api中的接口,不过很多人不知道具体的调用方法,在学习python中的requests库后,我们就可以很轻松的实现了。
1、说明
api接口调用是指使用python的requests库进行访问,基本上是get或post请求,有些接口会加密,然后必须使用对方提供给我们的公钥加密或解密,配上相应的参数进行访问,我们所需要的数据在请求后的返回结果中,所看到的基本上都是json格式的解析,所以请求后可以使用requests自带的json函数进行解析,然后提取所需的数据,访问一次就能得到一个数据。
2、实例
# encoding: utf-8
import requests
import os,re
import urllib.request
data={"email":"251910179@qq.com", "password":"ydd4903087"}
session = requests.session()
session.post("http://www.renren.com/PLogin.do",data= data,verify = False)
response =session.get("http://www.renren.com/410043129/profile")
print (response.text)
print (response.url)
print (response.status_code)
print (response.headers)
#爬网页图片:
requset=requests.post("http://tieba.baidu.com/p/4114581614",verify = False)
r=r'src="(http://imgsrc.baidu.com/.*?\.jpg)"'
#r=r'http://imgsrc.baidu.com/.+?\.jpg'
mylist=re.findall(r,str(requset.text))
print (mylist)
j=0
for i in mylist:
urllib.request.urlretrieve(i, "C:/Users/Administrator/Desktop/img1/"+str(j)+".jpg")
j+=1
实例代码扩展:
# coding:utf-8
import json
from urlparse import parse_qs
from wsgiref.simple_server import make_server
# 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。
def application(environ, start_response):
# 定义文件请求的类型和当前请求成功的code
start_response('200 OK', [('Content-Type', 'text/html')])
# environ是当前请求的所有数据,包括Header和URL,body,这里只涉及到get
# 获取当前get请求的所有数据,返回是string类型
params = parse_qs(environ['QUERY_STRING'])
# 获取get中key为name的值
name = params.get('name', [''])[0]
no = params.get('no', [''])[0]
# 组成一个数组,数组中只有一个字典
dic = {'name': name, 'no': no}
return [json.dumps(dic)]
if __name__ == "__main__":
port = 5088
httpd = make_server("0.0.0.0", port, application)
print "serving http on port {0}...".format(str(port))
httpd.serve_forever()
来源:https://www.py.cn/jishu/jichu/29660.html
标签:python,调用api
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用正则表达式找出不包含连续字符串abc的单词
2010-03-02 20:28:00
Python判断以什么结尾以什么开头的实例
2021-07-31 06:42:58
sklearn-SVC实现与类参数详解
2023-09-04 18:10:43
在Python中利用pickle保存变量的实例
2021-05-04 04:25:50
![](https://img.aspxhome.com/file/2023/2/68882_0s.jpg)
python 插入Null值数据到Postgresql的操作
2021-11-24 22:39:24
Python之tkinter组合框Combobox用法及说明
2022-09-27 20:29:59
![](https://img.aspxhome.com/file/2023/4/95854_0s.gif)
VUE3中watch监听使用实例详解
2024-05-29 22:42:16
python装饰器decorator介绍
2021-12-18 10:56:25
Python基于百度AI实现OCR文字识别
2023-09-26 22:14:29
![](https://img.aspxhome.com/file/2023/3/90503_0s.png)
pytorch程序异常后删除占用的显存操作
2021-04-03 22:33:38
微信小程序开发常见问题及解决方案
2024-04-18 09:35:04
![](https://img.aspxhome.com/file/2023/1/136441_0s.png)
python中子类继承父类的__init__方法实例
2022-05-27 19:37:21
MySQL8.0中的窗口函数的示例代码
2024-01-14 12:30:26
![](https://img.aspxhome.com/file/2023/2/128082_0s.png)
Python简单定义与使用字典dict的方法示例
2023-03-10 03:22:48
![](https://img.aspxhome.com/file/2023/8/93668_0s.jpg)
使用Visual Studio 2022开发前端的详细教程
2023-07-02 05:30:35
![](https://img.aspxhome.com/file/2023/5/137075_0s.png)
Numpy数据类型转换astype,dtype的方法
2021-02-15 00:58:14
ASP编程入门进阶(十三):AdRotator & Content Rotator
2008-09-24 17:47:00
有关div页面拖动、缩放、关闭、遮罩效果代码
2024-04-10 10:45:34
mysql binlog(二进制日志)查看方法
2024-01-18 02:30:12
Golang中数据结构Queue的实现方法详解
2024-04-26 17:20:00