2017-01-16 54 views
1

编码网址我试图使用生成的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上的打印结果是相同的,但字符串不同,但我不明白如何获得相同的字符串。

+0

阅读本post..http://stackoverflow.com/a/17912811/6107715 –

回答

1

n%之间的字符串application%2F中有一个隐藏的Unicode字符。只要删除它,它应该工作。

0

在您的第二个打印声明中,您无意中引用了第一个查询urlQuery而不是computedQuery。计算查询中有一个额外的空间,在修复print语句后变得很明显。

更新与修复和一对夫妇的意见如下代码:

# -*- 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" # space after application deleted 
 
computedQuery = viafBaseUrl + viafCommand + params 
 
print(computedQuery) # was urlQuery 
 
if computedQuery==urlQuery: 
 
    print("same strings") 
 
req = urllib.request.Request(computedQuery) 
 
with urllib.request.urlopen(req) as rep: 
 
    print("success")