因此,我正在處理與Flickr API進行通信的代碼塊。XML分析器語法錯誤
我在xml.parsers.expat.ExpatError
(下文)中收到'語法錯誤'。現在我無法弄清楚它是如何成爲Python模塊中的語法錯誤的。
我看到了關於維基百科API的另一個類似問題,它似乎返回XML的HTML intead。 Flickr API返回XML;而我也得到同樣的錯誤時,不應該有來自Flickr(如flickr.galleries.addPhoto
)
碼的響應:
def _dopost(method, auth=False, **params):
#uncomment to check you aren't killing the flickr server
#print "***** do post %s" % method
params = _prepare_params(params)
url = '%s%s/%s' % (HOST, API, _get_auth_url_suffix(method, auth, params))
payload = 'api_key=%s&method=%s&%s'% \
(API_KEY, method, urlencode(params))
#another useful debug print statement
#print url
#print payload
return _get_data(minidom.parse(urlopen(url, payload)))
TRACEBACK:
Traceback (most recent call last):
File "TESTING.py", line 30, in <module>
flickr.galleries_create('test_title', 'test_descriptionn goes here.')
File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1006, in galleries_create
primary_photo_id=primary_photo_id)
File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1066, in _dopost
return _get_data(minidom.parse(urlopen(url, payload)))
File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 928, in parse
result = builder.parseFile(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: syntax error: line 1, column 62
(代碼從http://code.google.com/p/flickrpy/在新的BSD許可下)
UPDATE:
print urlopen(url, payload)
== <addinfourl at 43340936 whose fp = <socket._fileobject object at 0x29400d0>>
做一個urlopen(url, payload).read()
返回HTML,這是很難在終端閱讀:「你還沒有登錄」 P,但我設法讓出
奇怪的部分是Flickr不應該在這裏返回任何東西,或者如果權限是一個問題,它應該返回一個99: User not logged in/Insufficient permissions
錯誤,因爲它與GET函數(我期望會使用有效的XML)一樣。
我登錄到Flickr(瀏覽器)和程序正確地與delete
權限認證(危險的,但我想避免權限問題。)
那麼我的猜測是,它給你一個非xml錯誤? – poolie 2010-09-08 06:20:26
不是一個真正的錯誤。 API_Key不在url中,所以它一定沒有意識到它是一個API調用,所以它認爲它是一個瀏覽器,並用網頁進行響應。 – avacariu 2010-09-09 00:07:30