Is there a preferred python module that could help me to send XML through a HTTP request and be able to parse the returning XML?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
One way would be to use urllib2
:
r = urllib2.Request("http://example.com", data="<xml>spam</xml>",
headers={'Content-Type': 'application/xml'})
u = urllib2.urlopen(r)
response = u.read()
Note that you have to set the content-type header, or the request will be sent application/x-www-form-urlencoded
.
If that's too complicated for you, then you can also use the requests
library.
For parsing the response lxml
is a great library, but elementtree
will also do.