UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position
import urllib.request, re, urllib.error
key = '短裙'
key = urllib.request.quote(key)
header = ('User-Agent','Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/58.0')
opener = urllib.request.build_opener() #创建一个opener对象
opener.addheaders = [header] #增加报头
urllib.request.install_opener(opener) #变成全局
for i in range(10):
try:
url = 'https://s.taobao.com/list?spm=a21bo.2017.201867-links-0.13.4183d6fb4IhjMg&q='+key+'&cat=16&style=grid&seller_type=taobao&bcoffset=12&s='+str(i*60)
data = urllib.request.urlopen(url).read().encode('utf-8','ignore').decode('latin1','ignore')
pat = 'pic_url":"//(.*?)"'
imgeurl = re.compile(pat).findall(data)
except urllib.error.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr(e,'reason'):
print(e.reason)
for j in range(len(imgeurl)):
thisimg = imgeurl[j]
thisimgeurl = "http://" + thisimg
file = str(i)+str(j)+'.jpg'
urllib.request.urlretrieve(thisimgeurl,file)
运行后,出现了UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30: ordinal not in range(256),应该怎么解决