UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0xff:尝试编码时无效的起始字节('utf-8')

时间:2019-07-14 05:06:10

标签: python http encoding urllib

当我尝试使用.encode('utf-8')时,会出现"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"

我正尝试抓取一些图像,但有一个问题使我感到困惑。我使用urllib.request.urlopen(url).read().decode('utf-8')来转换html页面以获取图像的url。以后我要保存它们。

但是,我将所有关于urlopen的代码放入了一个函数中,所以我不想更改它们。然后我想也许我可以使用encode('utf-8')将它们转换回去。之后,我不知道为什么,但是出现了异常。

import urllib.request as ur
import os

def getresponse(url):
    head = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
    req = ur.Request(url, None, head)
    response = ur.urlopen(req)
    result = response.read().decode('utf-8')
    return result


def saveimg(urlimg):
    img = getresponse(urlimg).encode('utf-8')
    file = open('xx.jpg', 'wb')
    file.write(img)
    file.close()


def downloadimg(url):
    os.chdir('/Users/xxxxxxx/Desktop')
    os.mkdir('xx')
    os.chdir('xx')
    saveimg(url)

downloadimg('https://cn.bing.com/th?id=OHR.WaterperryGardens_ZH-CN5767279278_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp')

我只想知道原因。

1 个答案:

答案 0 :(得分:0)

我可以用requests提出解决方案吗?

import os
import requests  # pip install requests

def download(url, headers, dest_folder, new_file_name):
    if not os.path.exists(dest_folder):
        os.makedirs(dest_folder)  # create folder if it does not exist

    filename = new_file_name.replace(" ", "_")  # be careful with file names
    file_path = os.path.join(dest_folder, filename)

    r = requests.get(url, headers=headers, stream=True)
    if r.ok:
        print("saving to", os.path.abspath(file_path))
        with open(file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024 * 8):
                if chunk:
                    f.write(chunk)
                    f.flush()
                    os.fsync(f.fileno())
    else:  # HTTP status code 4XX/5XX
        print("Download failed: status code {}\n{}".format(r.status_code, r.text))


headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
url = 'https://cn.bing.com/th?id=OHR.WaterperryGardens_ZH-CN5767279278_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp'
dest_folder = '/Users/xxxxxxx/Desktop/yyyy'
new_file_name = 'xx.jpg'

# save file to /Users/xxxxxxx/Desktop/yyyy/xx.jpg
# appropriate folders will be created if they do not exist
download(url, headers, dest_folder, new_file_name)