Leif160519的blog Leif160519的blog

——————

目录
Python3.6通过自带的urllib通过get或post方法请求url
/  

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

本文摘自:https://blog.csdn.net/qq5132834/article/details/78904974

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

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题Python3.6通过自带的urllib通过get或post方法请求url
作  者Leif160519
出  处https://github.icu/articles/2019/10/08/1570524870017.html
关于博主:坐标六朝古都南京,服务器运维工程师+桌面运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!