需要爬点资料,需要用python3的urllib.request.urlopen打开http://live.dbpedia.org/data/Pablo_Guzmán_(reporter).json
, 然后报错,
UnicodeEncodeError: 'ascii' codec can't encode character '\xe1' in position 20: ordinal not in range(128)
问题是出在name
“Pablo_Guzmán_(reporter)” ,里面含有 non-ASCII character。
解决办法是 使用quote() e.g: quote('/El Niño/') 变为 '/El%20Ni%C3%B1o/'.
from urllib.parse import quote
from urllib.request import urlopen
tmp = "http://live.dbpedia.org/data/"+quote(name)+".json"
url = urlopen(tmp)
#将json的网页load成dictionary
data = json.load(url)