Python3.6通过自带的urllib通过get或post方法请求url的实例

yipeiwu_com5年前Python基础

废话不多说,直接上代码:

# coding:utf-8
from urllib import request
from urllib import parse
url = "http://10.1.2.151/ctower-mall-c/sys/login/login.do"
data = {"id":"wdb","pwd":"wdb"}
params="?"
for key in data:
  params = params + key + "=" + data[key] + "&"
print("Get方法参数:"+params)
headers = {
  #heard部分直接通过chrome部分request header部分
  'Accept':'application/json, text/plain, */*',
  'Accept-Encoding':'gzip, deflate',
  'Accept-Language':'zh-CN,zh;q=0.8',
  'Connection':'keep-alive',
  'Content-Length':'14', #get方式提交的数据长度,如果是post方式,转成get方式:【id=wdb&pwd=wdb】
  'Content-Type':'application/x-www-form-urlencoded',
  'Referer':'http://10.1.2.151/',
  'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36'
}
data = parse.urlencode(data).encode('utf-8')
req = request.Request(url, headers=headers, data=data) #POST方法
#req = request.Request(url+params) # GET方法
page = request.urlopen(req).read()
page = page.decode('utf-8')
print(page)

以上这篇Python3.6通过自带的urllib通过get或post方法请求url的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

通过Python模块filecmp 对文件比较的实现方法

filecmp定义了两个函数,用于方便地比较文件与文件夹:     filecmp.cmp(f1, f2[, shallow]):  比较两个文件...

python实现的二叉树算法和kmp算法实例

主要是:前序遍历、中序遍历、后序遍历、层级遍历、非递归前序遍历、非递归中序遍历、非递归后序遍历 复制代码 代码如下:#!/usr/bin/env python#-*- coding:ut...

python实现最长公共子序列

python实现最长公共子序列

最长公共子序列python实现,最长公共子序列是动态规划基本题目,下面按照动态规划基本步骤解出来。 1.找出最优解的性质,并刻划其结构特征 序列a共有m个元素,序列b共有n个元素,如果a...

在Pycharm terminal中字体大小设置的方法

如下所示: file->settings->Editor->General->Console里面的console commands history size 以上...

python实现读Excel写入.txt的方法

因为今天要用到把Excel中的数据写入到.txt文件中,所以简单的写了个代码: import numpy as np import xlrd #打开excel文件 data= x...