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服 ...
随机推荐
- java基础第十六篇之多线程
1:线程的概念 进程(任务):一个正在运行的程序 进程的调度:CPU来决定什么时候该运行哪个进程 (时间片轮流法) 线程在一个应用程序中,同时,有多个不同的执行路径,是进程中的实际运作单位. 好处是提 ...
- 《Python自动化测试九章经》
Python是当前非常流行的一门编程语言,它除了在人工智能.数据处理.Web开发.网络爬虫等领域得到广泛使用之外,他也非常适合软件测试人员使用,但是,对于刚入行的测试小白来说,并不知道学习Python ...
- mssql 单引号插入到sqlserver的方法分享
转自:http://www.maomao365.com/?p=6740 摘要: 下文讲述sqlserver操作中遇到单引号的处理方法sqlserver 数据库中可以存储任何字符,因为在数据库中字符都是 ...
- itest(爱测试) 3.3.7 发布,开源BUG 跟踪管理& 敏捷测试管理软件
v3.3.7 下载地址 :itest下载 itest 简介:查看简介 V3.3.7 增加了 5个功能增强,和8个BUG修复 ,详情如下所述. 5个功能增强 :(1)任务看板中,除了显示任务外,增加测试 ...
- 201871010116-祁英红《面向对象程序设计(java)》第四周学习总结
博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...
- selenium中driver.close()和driver.quit()的不同点
driver.quit()与driver.close()的不同:driver.quit(): Quit this driver, closing every associated windows;dr ...
- 作业一(python初认识)
一.python发展历史 1989,为了度过圣诞假期,Guido开始编写Python语言编译器.Python这个名字来自Guido的喜爱的电视连续剧<蒙蒂蟒蛇的飞行马戏团>.他希望新的语言 ...
- shellnet运行train_val_seg.py
1.semantic3d数据集准备:prepare_semantic3d_data.py 11个测试数据集(.txt文件): 假如运行的是室外点云数据集“seg_semantic3d”,可能需要做以下 ...
- 【SpringCloud之pigx框架学习之路 】1.基础环境安装
[SpringCloud之pigx框架学习之路 ]1.基础环境安装 [SpringCloud之pigx框架学习之路 ]2.部署环境 1.Cmder.exe安装 (1) windows常用命令行工具 下 ...
- Flutter基础系列之入门(一)
1.Flutter是什么? 官方介绍:Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面. Flutter可以与现有的代码一起工作.在全世界,Flutter ...