2011-03-17 4 views
1

foo.com에 링크, 양식, 자산 URL (이미지/자바 스크립트)로 구성된 HTML 문서가 있습니다.HTML 문서의 URL을 변환 하시겠습니까?

프레임없이 bar.com에서 제공하고 싶습니다. 또한 모든 상대 URL을 호스트 이름이 "bar.com"인 절대 URL, 자산 URL 및 양식 작업 URL로 변환하려고합니다.

나는 foo.com에서 HTML doument를 가져 왔습니다. Nokogiri를 사용하여 URL을 변형하는 다음 단계는 무엇입니까?

답변

2

Nokogiri는 HTML/XML 파서입니다. official tutorial을 따라 문서를 구문 분석하는 방법을 찾을 수 있습니다.

require 'rubygems' 
require 'nokogiri' 
# Open the remote document, or from local file 
require 'open-uri' # load open-uri library if the input is from the Internet 
doc = Nokogiri::HTML(open(URL_OR_PATH_TO_DOCUMENT)) 

# Search for img tags: 
doc.css('img').each do |img| 
    # modify its attribute 
    img['src'] = "#{URL_PREFIX}/#{img['src']}" 
end 

# print the modified html 
puts doc.to_html 
1
require 'nokogiri' 
require 'open-uri' 

url = 'http://www.google.com' 
doc = Nokogiri::HTML(open(url)) 
doc.xpath('//a').each do |d| 
    rel_url = d.get_attribute('href') 
    d.set_attribute('href', 'http://www.xyz.com/' + rel_url) 
end 
: 여기

은 일례이며
관련 문제