Python 3 urllib produces TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str

前端 未结 3 377
轮回少年
轮回少年 2020-12-01 18:01

I am trying to convert working Python 2.7 code into Python 3 code and I am receiving a type error from the urllib request module.

I used the inbuilt 2to3 Python tool

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 18:28

    From the docs Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data:

    data = urllib.parse.urlencode(d).encode("utf-8")
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req,data=data) as f:
        resp = f.read()
        print(resp)
    

提交回复
热议问题