有誰知道是否有簡單的方法來記錄URL重定向路徑上的所有url?錄製url重定向
例如:url:(url 1)重定向到(url 2),重定向到(url 3)。
我想寫一個腳本,它接受一個輸入字符串(url 1)並返回(url 2)和(url 3)。
有沒有一個簡單的方法來做到這一點(理想情況下不使用JavaScript)?
謝謝!
有誰知道是否有簡單的方法來記錄URL重定向路徑上的所有url?錄製url重定向
例如:url:(url 1)重定向到(url 2),重定向到(url 3)。
我想寫一個腳本,它接受一個輸入字符串(url 1)並返回(url 2)和(url 3)。
有沒有一個簡單的方法來做到這一點(理想情況下不使用JavaScript)?
謝謝!
require 'net/http'
def redirect_tracker(url)
paths_array = [url]
code = nil
begin
response = Net::HTTP.start(URI.parse(url).host){|http| http.request Net::HTTP::Head.new(url) }
code = response.code.to_i
paths_array << url if url = response['location']
end while (301..303).include?(code)
return paths_array
end
redirect_tracker('http://nyti.ms/1gtu0MO')
# => ["http://nyti.ms/1gtu0MO", "http://nytimes.com/", "http://www.nytimes.com/"]
鑑於從LWP一個$response
,
my @request_uris;
while ($response) {
unshift @request_uris, $response->request->uri;
$response = $response->previous;
}
:
import requests
url = 'http://nyti.ms/1gtu0MO'
r= requests.get(url)
urls = [e.url for e in r.history]+[r.url]
那麼你得到:
>>>網址
[U 'http://nyti.ms/1gtu0MO',U 'http://nytimes.com/' U 'http://www.nytimes.com/']
它只能如果重定向在http層(30x範圍)
http://stackoverflow.com/questi ons/251532 /如何確定是否重定向 –
這可能有所幫助:https://www.ruby-forum.com/topic/142745 – Linuxios