python get请求 生成json_【python之requests模块使用01】安装、模拟get/post请求,jsonpath解析json数据...

一、安装

方式一:执行 pip install -U requests 联网安装requests

方式二:进入https://pypi.org/project/requests/,下载并解压requests-***.tar.gz,然后用cmd进入解压目录,使用命令Python setup.py

二、模拟get请求

1)不带参数

import requests

response = requests.get('http://www.hnxmxit.com/')

# print(response.content.decode('utf-8'))

response.encoding ='utf-8'

print(response.text)

2)带参数

import requests

get_param_data={

'grant_type':'client_credential',

'appid':'appid',

'secret':'secret'

}

response = requests.get('https://api.weixin.qq.com/cgi-bin/token',get_param_data)

# print(response.content.decode('utf-8'))

print(response.json()['access_token'])

3)加请求头

import requests

get_param_data = {'wd':'你好'}

headinfos = {

'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',

'Accept-Encoding':'gzip,deflate,br',

'Accept-Language':'zh-CN,zh;q=0.9',

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'

}

response = requests.get(url='https://www.baidu.com/s',params=get_param_data,headers=headinfos)

print(response.content.decode('utf-8'))

三、模拟post请求

Post 请求的请求参数是通过data方式来传递的。Post的请求参数一般有form表单(使用dict类型传输)、json数据、文件等。

1)form表单:(新建用户标签)

import requests

import json

get_param_data ={'access_token':'33_cmNwYXdi1m3_bCbx2Y3jhvTsBGWNol_jVVy7X8tJ4kSn4M9vc33TlKfCjWFoClH8Qkt_dhagALj1T4IVlhnR8H7mz65HJTNwDWpAG4I4Gk7OGq1VSuyXhq2NRjnqWACqoPBSDmxnphzSocpMDZKjADAXTE'}

post_param_date = { "tag" : { "id" : 100, "name" : "广东人人" } }

headinfos = {'Content-Type': 'application/json'} #发送json数据必带的头部信息

reponse = requests.post(url='https://api.weixin.qq.com/cgi-bin/tags/update',

params=get_param_data,

data=json.dumps(post_param_date),

headers=headinfos)

print(reponse.content.decode('utf-8'))

2)Json数据:

方式一: (需要使用json模块)

jsonParams = {'key': 'value'}

headers = {'Content-Type': 'application/json'}

postJsonResponse = requests.post(url, headers=headers,data=json.dumps(jsonParams))

方式二:

jsonParams = {'key': 'value'}

headers = {'Content-Type': 'application/json'}

postJsonResponse = requests.post(url, headers=headers,json=jsonParams)

3)文件上传:

import requests

# 简单处理

excel_file ={'file':open('file1.xlsx','rb')}

# 显式地设置文件名,文件类型和请求头:(文件名,文件的打开方式,文件头信息,文件有效期)

# excel_file ={'file':('file1.xlsx',open('file1.xlsx','rb'),'application/vnd.ms-excel',{'Expires': '0'})}

file_response = requests.post('http://httpbin.org/post',files=excel_file)

print(file_response.content.decode('utf-8'))

四、json和jsonpath

import json

str_dict={'name':'xiaoming','age':22}

str1 = json.dumps(str_dict) # josn-->字符串

str2 =json.loads(str1) # 字符串 --》字典

print(str2['age'])

解析json数据

import jsonpath

str_value = '{"tags":[{"id":2,"name":"星标组","count":0},{"id":100,"name":"广东人","count":3},{"id":101,"name":"湖南","count":0},{"id":103,"name":"test001","count":0},{"id":183,"name":"测试05-29-hello_01","count":0},{"id":184,"name":"测试05-29-hello_02","count":0},{"id":185,"name":"测试05-29-hello_03","count":0},{"id":186,"name":"测试05-29-hello_04","count":0},{"id":187,"name":"测试05-29-hello_05","count":0}]}'

json_data = json.loads(str_value) #josn对象

value = jsonpath.jsonpath(json_data,'$.tags[1].name')

print(value,value[0])

输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值