Python简单的get和post请求
1.json
模块提供了一种很简单的方式来编码和解码JSON数据。 其中两个主要的函数是 json.dumps()
和 json.loads()
, 要比其他序列化函数库如pickle的接口少得多。 下面演示如何将一个Python数据结构转换为JSON:
import json
data = {
'name' : 'ACME',
'shares' : 100,
'price' : 542.23
}
json_str = json.dumps(data)
下面演示如何将一个JSON编码的字符串转换回一个Python数据结构:
data = json.loads(json_str)
2.简单的get和post请求,使用import requests
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
#通过在发送post请求时添加一个data参数,这个data参数可以通过字典构造成
import requests
data = {
"name":"zhaofan",
"age":23
}
response = requests.post("http://httpbin.org/post",data=data)
print(response.text)
3.GET方法,并且自定义header
# -* - coding: UTF-8 -* -
import urllib2
request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()
POST方法,并且自定义header
# -* - coding: UTF-8 -* -
import urllib2
import urllib
request = urllib2.Request("http://passport.cnblogs.com/login.aspx")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"}
response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()
4.实际测试脚本编写
# coding:utf-8
import json
import urllib2
import requests
class AddScores:
def __init__(self):
pass
def getToken(self): # 获取token值
url1 = 'xxxxx'#url
r1 = requests.get(url1)
self.tokenObj = json.loads(r1.text)#解码JSON数据
if self.tokenObj["result"] == "success":
print self.tokenObj["token"]
else:
print "failed"
return self.tokenObj["token"]
def personMess(self): # 获取个人信息
url2 = 'xxx' + self.getToken()
r2 = requests.post(url2)
print r2.text
def addSco(self,resId): # 添加分数
data = {
"memberId": "xxx",
"orgCode": "xxx",
"resourceId": resId,#传参,传resourceId
"configName": "wsp", "resourceType": "wsp"
}
print "添加分数的请求参数:"
print json.dumps(data)#编码JSON
headers = {'Content-Type': 'application/json'}
url3 = 'xxx' + self.getToken()
re3 = urllib2.Request(url=url3, headers=headers, data=json.dumps(data))
response = urllib2.urlopen(re3)
print response.read()
5.读写TXT文件
#coding:utf-8
import time
from Demo2.token import AddScores
class ResId:
def getResId(self):
file=open('xxxx')
# a=file.read()
# print a
lId= file.readline()
lId=lId.strip(',\n')
while lId != '':#逐行读取数据
print lId
addScores = AddScores()
addScores.getToken()
addScores.personMess()
addScores.addSco(lId)
time.sleep(68)
lId = file.readline()
print "============================="
ResId().getResId()
Python简单的get和post请求的更多相关文章
- python web.py实现简单的get和post请求
使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...
- python之简单的get和post请求
1.json 模块提供了一种很简单的方式来编码和解码JSON数据. 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多. ...
- Python简单爬虫入门三
我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...
- Python简单爬虫入门二
接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...
- GJM : Python简单爬虫入门(二) [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- Selenium + PhantomJS + python 简单实现爬虫的功能
Selenium 一.简介 selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操作一样 selenium2支持通过驱动真实浏览器(FirfoxDrive ...
- python 简单图像识别--验证码
python 简单图像识别--验证码 记录下,准备工作安装过程很是麻烦. 首先库:pytesseract,image,tesseract,PIL windows安装PIL,直接exe进行安装更方便( ...
- python 简单搭建非阻塞式单进程,select模式,epoll模式服务
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里 可以看我的上篇文章 <python 简单搭建阻塞式单进程,多进程, ...
- python 简单搭建阻塞式单进程,多进程,多线程服务
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...
随机推荐
- python基础—条件语句
一.Python基础 1.第一句python print('hello,world') Q: 后缀名可以任意? A: 导入模块时,如果不是.py后缀,会出错. 2.两种执行的方式: -python解 ...
- JavaScript 正则表达式 初探
JavaScript 正则表达式 正则表达式是构成搜索模式的字符序列 搜索模式可用于文本搜索和文本替换操作 使用正则 字符串方法 在JavaScript中,正则表达式常常用两个字符串方法: searc ...
- Android自定义注解
1.元注解 概念:用来定义其他注解的注解,自定义注解的时候,需要使用它来定义我们的注解. 在jdk 1.5之后提供了 java.lang.annotation 来支持注解功能 常见的四种元 ...
- 外置 tomcat 服务器设置
外置 Tomcat 没这么太用, 今天在 windows 搭 xwiki 服务器, 比预期多花了点时间, 主要是 tomcat 环境变量没配对, tomcat 启动后闪退, 还没有日志. 最后定位 ...
- [b0029] python 归纳 (十四)_队列Queue实现生产者消费者
# -*- coding: UTF-8 -*- """ 多线程的生产者,消费者 使用队列Queue """ import Queue imp ...
- Linux—主机扫描工具(Nmap)
Nmap包含五项基本功能: 主机探测 (Host Discovery) 端口扫描 (Port Scanning) 版本检测 (Version Detection) 操作系统侦测 (Operating ...
- centos 的系统管理命令 service systemctl
centos 的 systemctl 命令 systemctl is-enabled *.service #查询服务是否开机启动 systemctl enable *.service # ...
- 洛谷P2260 [清华集训2012]模积和(容斥+数论分块)
题意 https://www.luogu.com.cn/problem/P2260 思路 具体思路见下图: 注意这个模数不是质数,不能用快速幂来求逆元,要用扩展gcd. 代码 #include< ...
- 常见的Dos命令大全
打开cmd: Win键+R 输入cmd; 常用的Dos命令: 1.盘符切换: 2.打开文件目录: dir 3.清理屏幕: cls 4.退出: exit 5.查看本机IP地址:ipconfig ...
- zabbix使用钉钉告警
1.钉钉创建群 2.[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf # 配置文件中查找”Alert”查看告警脚本存放路径 [root@lo ...