我正在尝试使用像这样的生成的url
http://www.viaf.org/viaf/search?query=cql.any+=+%22Jean-Claude%20Moissinac%22&maximumRecords=5&httpAccept=application/json
但是当它和
# -*- 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")
字符串
application%2F
中在n
和%
之间有一个隐藏的unicode字符。只要删除它,它应该可以工作。在在第二个print语句中,意外地引用了第一个查询}。在修复print语句之后,计算查询中有一个额外的空间,这一点变得明显。在
urlQuery
,而不是{更新了下面的代码,其中包含了修复程序和一些注释:
# -*- 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")
;