问题
>>> import gdata.books.service
>>> service = gdata.books.service.BookService()
>>> results = service.search_by_keyword(isbn='0434003484')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
results = service.search_by_keyword(isbn='0434003484')
... snip ...
File "C:\Python26\lib\site-packages\atom\__init__.py", line 127, in CreateClassFromXMLString
tree = ElementTree.fromstring(xml_string)
File "<string>", line 85, in XML
SyntaxError: syntax error: line 1, column 0
This is a minimal example -- in particular, the book service unit tests included in the package also fail with the exact same error. I've looked at the wiki and open issue tickets on Google Code to no avail (and this seems to me more apt to be a silly error on my end rather than a problem with the library). I'm not sure how to interpret the error message. If it matters, I'm using python 2.6.5 and the latest version of gdata, namely 2.0.10.
回答1:
I found I needed to disable SSL in the gdata client for it to work:
...
gd_client.ProgrammaticLogin()
gd_client.ssl = False
...
回答2:
sje397's answer is the correct one; in your example above, if you do:
service.ssl = False
before running the search_by_keyword method, the result is properly returned. If you don't set SSL to be false, then because the client hasn't properly authenticated, the request is redirected to the main Google homepage (hence what Alex Martelli was seeing errors in was the HTML you get when you go to http://www.google.com ... definitely not well-formed XML).
回答3:
Hacking around a bit to see the xml string, I notice it has a lowercase <!doctype html>
at the start (should be uppercase DOCTYPE
) which of course is making the XML parse fail -- definitely a bug in the book service which needs to be reported. As a temporary workaround you could hack the atom/__init__.xml
to change line 127 to...:
tree = ElementTree.fromstring(xml_string.replace('doctype','DOCTYPE'))
but while that gets past this bug in the XML it reveals another at column 496 (I think that's in the middle of some javascript code). I guess there's something borken in bookservice in general at this point...:-(
来源:https://stackoverflow.com/questions/2925985/syntaxerror-using-gdata-python-client-to-access-google-book-search-data-api