我的代码卡在这个函数调用上:
feedparser.parse("http://...")
这以前有效。该网址甚至无法在浏览器中打开。 你会如何解决这个案子?有超时的可能吗?我想继续,就好像什么都不会发生一样(仅打印一些消息或记录此问题)
最佳答案
使用 Python requests
库进行网络 IO,feedparser 仅用于解析:
# Do request using requests library and timeout
try:
resp = requests.get(rss_feed, timeout=20.0)
except requests.ReadTimeout:
logger.warn("Timeout when reading RSS %s", rss_feed)
return
# Put it to memory stream object universal feedparser
content = BytesIO(resp.content)
# Parse content
feed = feedparser.parse(content)
关于python - 带超时的 feedparser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9772691/