我使用的是Erlang的ibrowse API。问题是,当我试图阅读重定向到另一个网页的网页时,结果是空白的。有没有关于如何跟踪跟踪到最后一页的想法?
这是我目前所拥有的:
get_web(Src) ->
ibrowse:start(),
{_,_,_,Body} = ibrowse:send_req(Src, [], get),
Body.谢谢
发布于 2012-09-19 22:45:48
如果响应具有301或302状态代码,则应遵循响应中的Location标头。
get_web({url,Src}) ->
ibrowse:start(),
{ok, Status, Head, Body} = ibrowse:send_req(Src, [], get),
if
Status =:= 200 ->
Body;
Status =:= 301 orelse Status =:= 302 ->
get_web(get_location(Head))
end.
get_location(Head) ->
case lists:keyfind("Location", 1, Head) of
false -> {url, error};
URL -> {url, URL}
end.发布于 2012-09-19 22:11:14
如果有人偶然发现了这一点,我是这么做的:
get_web({_,error}) ->
error;
get_web({url,Src}) ->
ibrowse:start(),
{_,_,Head,Body} = ibrowse:send_req(Src, [], get),
if
length(Body) == 0 ->
get_web(get_location(Head));
true ->
Body
end.
get_location([]) ->
{url,error};
get_location([{"Location",URL}|_]) ->
{url,URL};
get_location([_|T]) ->
get_location(T).https://stackoverflow.com/questions/12495355
复制相似问题