Python简单的get和post请求

  • 2022-07-21 19:00:37

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()

猜你喜欢