用原生urllib.request模拟post请求
from urllib import request, parse,error
import json
def get_page(url):
headers = {
'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 66.0.3359.139Safari / 537.36',
'Connection': 'keep-alive',
"Referer": "http://test.m.youpenglai.cn/login",
'Content-Type': 'application/json;charset=UTF-8',
'Host':"test.m.youpenglai.cn"
}
data = {
'loginname': '15057769636',
'loginpassword': '123456',
'logintype': '1'
}
# kets = "-"
# val = parse.quote(kets)
# url = url + val
data = parse.urlencode(data).encode('utf-8')
req = request.Request(url,data,headers,method='POST')
print(req)
try:
page = request.urlopen(req).read()
page = page.decode('utf-8')
print(page)
with open("./test.txt","wb+") as f:
f.write(page)
except error.HTTPError as e:
print(e.code())
return page
get_page('http://test.m.youpenglai.cn/y...')
错误提示是这个
urllib.error.HTTPError: HTTP Error 400: Bad Request