我正在开发一个Ruby脚本,它可以自动从用户或术语列表中收集tweet,除非Twitter容量不足,否则它可以正常工作。然后,Twitter返回一个没有错误代码的HTML页面供我捕获。HTML抛出解析器,脚本就失败了。如何检查HTML响应并对其进行适当处理?我尝试使用“直到成功”的方法(在下面的代码中注释掉),但每次都以拯救告终。
我正在使用记录器、twitter和twitter4r gem,并通过OAuth进行身份验证。下面的代码可以正常工作,除非Twitter的容量不足。只有在没有返回RestErrors的情况下,我才会进入这个部分。
下面是我的代码:
# until success
# begin
if search_type == "users"
# begin
tweets_array = client.timeline_for(:user, :id => row.chomp, :since_id => since_status_id, :count => 200)
success = true
# rescue
# log.info("error getting tweets. waiting to try again.")
# sleep 180
# end
elsif search_type == "terms"
# begin
tweets_array = client.search(:q => row.chomp, :since_id => since_status_id, :count => 200)
success = true
# rescue
# log.info("error getting tweets. waiting to try again.")
# sleep 180
# end
elsif
log.fatal("unsupported search type. exiting.")
break
end # search type
# end 我正在尝试的基于答案的代码:
begin
tweets_array = client.timeline_for(:user, :id => row.chomp, :since_id => since_status_id, :count => 200)
success = true
rescue Twitter::RESTError => re
log.info(row.chomp + ": " + re.code.to_s + " " + re.message.to_s + " " + re.uri.to_s)
if re.code = 503
sleep 180
end
end发布于 2011-05-27 08:01:21
你可以检查结果的HTTP status code,应该是503。
http://twitter.com/503
https://stackoverflow.com/questions/6142042
复制相似问题