Questions about Python 3 urlib login!

Asked 1 years ago, Updated 1 years ago, 122 views

from urllib.parse import urlencode
from urllib.request import Request,urlopen
from urllib import *

url = "https://dshs.kr:44919/bbs/login.php" #daeshin high
login_form = {"Member ID":"_____","Password":"____"}
login_req = urlencode(login_form)#convert dictionary to query string
request = Request(url, login_req)
response = urlopen(request)

The top part is the chords I made The following error appears:

Traceback (most recent call last):
  File "C:\Users\SeungYoun\Desktop\URL_Request.py", line 9, in <module>
    response = urlopen(request)
  File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 461, in open
    req = meth(req)
  File "C:\Python34\lib\urllib\request.py", line 1112, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
>>> 

The question here is

I don't know what the following means, so I don't know why the error appears.

request = Request (url, login_req)

urllib python3

2022-09-22 14:50

1 Answers

If you read the error, the POST value cannot be in str format, but only bytes format.

If you encode the code ('utf-8) in utf-8 format, it will change to bytes

from urllib.parse import urlencode
from urllib.request import Request,urlopen
from urllib import *

url = "https://dshs.kr:44919/bbs/login.php" #daeshin high
login_form = {"Member ID":"_____","Password":"____"}

# This is the STR format. STR is not allowed
login_req = urlencode(login_form)#convert dictionary to query string

# When encoded in UTF-8 format, it changes to bytes
login_req = login_req.encode('utf-8')

request = Request(url, login_req)

response = urlopen(request)

I shouldn't have added it like this~


2022-09-22 14:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.