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

yipeiwu_com6年前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使用filetype精确判断文件类型

filetype.py Small and dependency free Python package to infer file type and MIME type checkin...

Python安装使用命令行交互模块pexpect的基础教程

一、安装 1、安装easy_install工具 wget http://peak.telecommunity.com/dist/ez_setup.py python ez_se...

python 捕获shell脚本的输出结果实例

import subprocess output =Popen(["mycmd","myarg"], stdout=PIPE).communicate()[0] import subp...

Python WSGI的深入理解

前言 本文主要介绍的是Python WSGI相关内容,主要来自以下网址: What is WSGI? WSGI Tutorial An Introduction t...

深入讲解Python中的迭代器和生成器

深入讲解Python中的迭代器和生成器

在Python中,很多对象都是可以通过for语句来直接遍历的,例如list、string、dict等等,这些对象都可以被称为可迭代对象。至于说哪些对象是可以被迭代访问的,就要了解一下迭代...