Sto usando Nokogiri per estrarre i collegamenti da una pagina ma vorrei ottenere il percorso assoluto anche se quello sulla pagina è relativo. Come posso realizzare questo?Come posso ottenere l'URL assoluto quando estrae i collegamenti usando Nokogiri?
risposta
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.
È 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.
risposta Phrogz' va bene, ma più semplicemente:
URI.join(base, url).to_s
Puoi dare un esempio di cosa sono base e url? – lulalala
'base =" http://www.google.com/somewhere "; url = '/ over/there'; 'Credo che i nomi delle variabili di pguardino siano un po 'imprecisi –
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