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 __getattr__与__setattr__使用方法

比如下面的例子: class Book(object):    def __setattr__(self, name, value):  ...

在Python中构建增广矩阵的实现方法

麻烦的 # TODO 构造增广矩阵,假设A,b行数相同 def augmentMatrix(A, b): if(len(A) != len(b)): raise 'The...

在Python中使用sort()方法进行排序的简单教程

 sort()方法排序列表中的对象,比较使用func(如果给定)。 语法 以下是sort()方法的语法: list.sort([func]) 参数  ...

Python 文件处理注意事项总结

Python 文件处理注意事项总结 文件处理在编程中是常见的操作,文件的打开,关闭,重命名,删除,追加,复制,随机读写非常容易理解和使用。需要注意的是文件的安全关闭,采用with语句轻松...

python多线程方式执行多个bat代码

python多线程方式执行多个bat的代码,感兴趣的朋友可以参考下。 import threading from win32api import * class MyThread...