2011-02-01 13 views

risposta

55

Nokogiri non è correlato, a parte il fatto che fornisce l'ancoraggio del collegamento per iniziare. Utilizzare biblioteca di Ruby URI per gestire percorsi:

absolute_uri = URI.join(page_url, href).to_s 

visto in azione:

require 'uri' 

# The URL of the page with the links 
page_url = 'http://foo.com/zee/zaw/zoom.html' 

# A variety of links to test. 
hrefs = %w[ 
    http://zork.com/    http://zork.com/#id 
    http://zork.com/bar   http://zork.com/bar#id 
    http://zork.com/bar/   http://zork.com/bar/#id 
    http://zork.com/bar/jim.html http://zork.com/bar/jim.html#id 
    /bar       /bar#id 
    /bar/      /bar/#id 
    /bar/jim.html    /bar/jim.html#id 
    jim.html      jim.html#id 
    ../jim.html     ../jim.html#id 
    ../       ../#id 
    #id 
] 

hrefs.each do |href| 
    root_href = URI.join(page_url,href).to_s 
    puts "%-32s -> %s" % [ href, root_href ] 
end 
#=> http://zork.com/     -> http://zork.com/ 
#=> http://zork.com/#id    -> http://zork.com/#id 
#=> http://zork.com/bar    -> http://zork.com/bar 
#=> http://zork.com/bar#id   -> http://zork.com/bar#id 
#=> http://zork.com/bar/    -> http://zork.com/bar/ 
#=> http://zork.com/bar/#id   -> http://zork.com/bar/#id 
#=> http://zork.com/bar/jim.html  -> http://zork.com/bar/jim.html 
#=> http://zork.com/bar/jim.html#id -> http://zork.com/bar/jim.html#id 
#=> /bar        -> http://foo.com/bar 
#=> /bar#id       -> http://foo.com/bar#id 
#=> /bar/       -> http://foo.com/bar/ 
#=> /bar/#id       -> http://foo.com/bar/#id 
#=> /bar/jim.html     -> http://foo.com/bar/jim.html 
#=> /bar/jim.html#id     -> http://foo.com/bar/jim.html#id 
#=> jim.html       -> http://foo.com/zee/zaw/jim.html 
#=> jim.html#id      -> http://foo.com/zee/zaw/jim.html#id 
#=> ../jim.html      -> http://foo.com/zee/jim.html 
#=> ../jim.html#id     -> http://foo.com/zee/jim.html#id 
#=> ../        -> http://foo.com/zee/ 
#=> ../#id       -> http://foo.com/zee/#id 
#=> #id        -> http://foo.com/zee/zaw/zoom.html#id 

La risposta più contorto qui in passato utilizzato URI.parse(root).merge(URI.parse(href)).to_s.
Grazie a @pguardiario per il miglioramento.

+4

Nokogiri potrebbe essere correlato a questo. Ecco come: se un documento html contiene un tag base, la soluzione sopra non funzionerà correttamente. In tal caso, il valore dell'attributo href del tag di base deve essere utilizzato al posto di page_url. Dai un'occhiata alla spiegazione più dettagliata di @ david-thomas qui: http://stackoverflow.com/questions/5559578/havling-links-relative-to-root – draganstankovic

1

È necessario verificare se l'URL è assoluto o relativo con controllo se iniziale da http: Se l'URL è relativo è necessario aggiungere l'host a questo URL. Non puoi farlo con nokogiri. È necessario elaborare tutto l'URL interno per renderlo come assoluto.

14

risposta Phrogz' va bene, ma più semplicemente:

URI.join(base, url).to_s 
+2

Puoi dare un esempio di cosa sono base e url? – lulalala

+1

'base =" http://www.google.com/somewhere "; url = '/ over/there'; 'Credo che i nomi delle variabili di pguardino siano un po 'imprecisi –

Problemi correlati