simulate post requests with native urllib.request
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"
}
-sharp kets = "-"
-sharp val = parse.quote(kets)
-sharp 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.")
the error message is this
urllib.error.HTTPError: HTTPError 400: Bad Request