想用asyncHttpClient来下载xml,并用feedParser解析。代码如下
# coding:utf8
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop
import feedparser
def parse(response):
if response.error:
print "Error:", response.error
else:
print feedparser.parse(response.body).feed.title
http_client = AsyncHTTPClient()
http_client.fetch("http://www.laruence.com/feed", parse)
http_client.fetch("http://feed.cnblogs.com/blog/sitehome/rss", parse)
http_client.fetch("http://feed.cnblogs.com/blog/u/155819/rss", parse)
IOLoop.instance().start()
直接print response.body没有任何问题,但是加上feedParser来处理后,就会出现异常。似乎是解析时间过长导致的。有什么解决办法吗?
博客园_FX的笔记
博客园_首页
WARNING:tornado.general:uncaught exception
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado\stack_context.py", line 239, in wrapped
callback(*args, **kwargs)
File "C:\Python27\lib\site-packages\tornado\simple_httpclient.py", line 212, in _on_timeout
raise HTTPError(599, "Timeout")
HTTPError: HTTP 599: Timeout
Error: HTTP 599: Timeout
ERROR:tornado.application:Uncaught exception, closing connection.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 316, in wrapper
callback(*args)
File "C:\Python27\lib\site-packages\tornado\stack_context.py", line 239, in wrapped
callback(*args, **kwargs)
File "C:\Python27\lib\site-packages\tornado\simple_httpclient.py", line 470, in _on_chunk_data
self.stream.read_until(b"\r\n", self._on_chunk_length)
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 148, in read_until
self._try_inline_read()
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 393, in _try_inline_read
self._check_closed()
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 543, in _check_closed
raise StreamClosedError("Stream is closed")
StreamClosedError: Stream is closed
ERROR:tornado.application:Exception in callback <functools.partial object at 0x02F12A20>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado\ioloop.py", line 453, in _run_callback
callback()
File "C:\Python27\lib\site-packages\tornado\stack_context.py", line 241, in wrapped
callback(*args, **kwargs)
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 316, in wrapper
callback(*args)
File "C:\Python27\lib\site-packages\tornado\stack_context.py", line 239, in wrapped
callback(*args, **kwargs)
File "C:\Python27\lib\site-packages\tornado\simple_httpclient.py", line 470, in _on_chunk_data
self.stream.read_until(b"\r\n", self._on_chunk_length)
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 148, in read_until
self._try_inline_read()
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 393, in _try_inline_read
self._check_closed()
File "C:\Python27\lib\site-packages\tornado\iostream.py", line 543, in _check_closed
raise StreamClosedError("Stream is closed")
StreamClosedError: Stream is closed
访问超时了而已。你网络状况不太好,调整下超时时间?