python requests.get() returns improperly decoded text instead of UTF-8?

前端 未结 4 2086
野性不改
野性不改 2020-11-29 22:57

When the content-type of the server is \'Content-Type:text/html\', requests.get() returns improperly encoded data.

However, if

4条回答
  •  情深已故
    2020-11-29 23:42

    After getting response, take response.content instead of response.text and that will be of encoding utf-8.

    response = requests.get(download_link, auth=(myUsername, myPassword),  headers={'User-Agent': 'Mozilla'})
    print (response.encoding)
    if response.status_code is 200:
        body = response.content
    else:
        print ("Unable to get response with Code : %d " % (response.status_code))
    

提交回复
热议问题