某些下载后发生unicode冲突

时间:2018-06-22 02:04:53

标签: http url utf-8

我有一个.txt文件( pos_images.txt ),其中包含来自不同站点的图像网址。

http://farm4.static.flickr.com/3644/3349801965_d0268de7f5.jpg
http://farm1.static.flickr.com/15/22639560_8edd3afe6f.jpg
http://farm1.static.flickr.com/94/223377572_1a2590c521.jpg
http://farm3.static.flickr.com/2325/2246167212_ff39c4d699.jpg
http://fotosa.ru/stock_photo/Westend61_RM/p_2838903.jpg
http://farm2.static.flickr.com/1160/536319721_cc51452ad5.jpg
http://farm4.static.flickr.com/3086/2336674146_71fa578fb5.jpg
http://farm1.static.flickr.com/181/461311965_b430187841.jpg
http://farm4.static.flickr.com/3143/3083759691_ebdc397703.jpg
http://farm4.static.flickr.com/3574/3321180115_c61e8feaa7.jpg
http://www.zingem.be/download/uitleenformulier/veiligheidshelm.jpg

我有以下代码可以下载它们:

import cv2
from six.moves import urllib
import numpy as np
import os

with open("pos_images.txt", 'rb') as f:
    images = f.readlines()
    images = [x.strip().decode("utf-8") for x in images]
    index = 1
    for image in images:
        pos_image_urls = urllib.request.urlopen(image).read()

        try:
            urllib.request.urlretrieve(image, 'pos/'+str(index)+'.jpg')
            img = cv2.imread('pos/'+str(index)+'.jpg')
            index += 1

        except Exception as e: 
            print(str(e))

当我运行此代码时,它会将.txt文件中的前四个图像下载并引发错误

我以前从未遇到过此错误,并且在使用.decode()进行了一些小的更改之后无法解决它。我不了解网络上的其他解决方案。

有人可以帮助解决此问题并解释其含义吗?

0 个答案:

没有答案