编码网址我试图使用生成的URL像这样的 http://www.viaf.org/viaf/search?query=cql.any+=+%22Jean-Claude%20Moissinac%22&maximumRecords=5&httpAccept=application/json与Python 3
但
# -*- encoding: utf-8 -*-
import urllib.request
# successful trial with the URI
urlQuery = u'http://www.viaf.org/viaf/search?query=cql.any%20=%20"Bacache%20Maya"&httpAccept=application%2Fjson&maximumRecords=5'
print(urlQuery)
req = urllib.request.Request(urlQuery)
with urllib.request.urlopen(req) as rep:
print("success")
# attempt to build the URI; request fails
viafBaseUrl = u"http://www.viaf.org"
viafCommand = u"/viaf/search?"
viafSearchTemplate = u'"__name%20__surname"'
name = u"Bacache"
surname = u"Maya"
searchString = u'cql.any%20=%20' + viafSearchTemplate.replace(u"__surname", surname).replace(u"__name", name)
params = u"query="+searchString+u"&httpAccept=application%2Fjson&maximumRecords=5"
computedQuery = viafBaseUrl + viafCommand + params
print(urlQuery)
if computedQuery==urlQuery:
print("same strings")
req = urllib.request.Request(computedQuery)
with urllib.request.urlopen(req) as rep:
print("success")
第一个请求是成功使用它时,而第二次失败并出现此错误:
UnicodeEncodeError: 'ascii' codec can't encode character '\ufeff' in position 76: ordinal not in range(128)
我尝试了很多方法来解决问题而没有成功。 使用urllib.parse.urlencode()
失败,因为它改变了一些必须保持不变的字符。
这两个url上的打印结果是相同的,但字符串不同,但我不明白如何获得相同的字符串。
阅读本post..http://stackoverflow.com/a/17912811/6107715 –