2010-05-26 4 views
0

Je dispose d'un document html situé sur http://somedomain.com/somedir/example.htmlNokogiri trouver seulement des liens entrants

Le document contient quatre liens:

http://otherdomain.com/other.html

http://somedomain.com/other.html

/only.html

tests .html

Comment puis-je obtenir les URL complètes pour les liens dans le domaine actuel?

Je veux dire que je devrais obtenir:

http://somedomain.com/other.html

http://somedomain.com/only.html

http://somedomain.com/somedir/test.html

Le premier lien doit être ignorée car il does'nt mon domaine correspondent

Répondre

0

utiliser régulièrement expression pour extraire les liens de href = "URL" puis concaténer nate avec le domaine si elle ne commence pas par "http"

Voici un exemple Python:

import re 
import urlparse 

domain = ... 
html = ... 
links = re.findall('href=[\'"](.*?)[\'"]', html) 
links = [urlparse.urljoin(domain, link) for link in links if link] 
1

Quelque chose comme

doc.search("a").map do |a| 
    url = a.attribute("href") 
    #this part could be a lot more robust, but you get the idea... 
    full_url = url.match("^http://") ? url : "http://somedomain.com/#{url}" 
end.select{|url| url.match("^http://somedomain.com")} 
Questions connexes