2012-07-05 10 views
5

Ayrıştırma HTTPResponse ile HTTPResponse ayrıştırma, ben yaşıyorum sorun HTTPResponse Nokogiri nesneleri ayrıştırma. Bu html baskılar yaptığınızda bir bütün anlamsız demet ve Nokogiri node_set bir Nokogiri olmalı" yakınıyor bir bağlantı Yakut Nokogiri</p> <p>Merhaba ile Nokogiri

def fetch(uri_str, limit = 10) 


    # You should choose better exception. 
    raise ArgumentError, 'HTTP redirect too deep' if limit == 0 

    url = URI.parse(URI.encode(uri_str.strip)) 
    puts url 

    #get path 
    req = Net::HTTP::Get.new(url.path,headers) 
    #start TCP/IP 
    response = Net::HTTP.start(url.host,url.port) { |http| 
     http.request(req) 
    } 
    case response 
    when Net::HTTPSuccess 
    then #print final redirect to a file 
    puts "this is location" + uri_str 
    puts "this is the host #{url.host}" 
    puts "this is the path #{url.path}" 

    return response 
    # if you get a 302 response 
    when Net::HTTPRedirection 
    then 
    puts "this is redirect" + response['location'] 
    return fetch(response['location'],aFile, limit - 1) 
    else 
    response.error! 
    end 
end 




      html = fetch("http://www.somewebsite.com/hahaha/") 
      puts html 
      noko = Nokogiri::HTML(html) 

getirme

:

Burada bir web sitesi getirmek için bu işlevi kullanın :: XML :: nodeset

kimse yardım teklif olsaydı oldukça

mutluluk duyacağız

+1

yerine bu sıcak karmaşa motorize kullanmak gerekir. Yönlendirmeler ve sizin için kodlamalarla ilgilenir. – pguardiario

cevap

4

ilk şey. Sizin fetch yöntemi, yalnızca gövde için değil, Net::HTTPResponse nesnesini döndürür. Vücudu Nokogiri'ye vermelisin.

response = fetch("http://www.somewebsite.com/hahaha/") 
puts response.body 
noko = Nokogiri::HTML(response.body) 

Komut dosyanızı güncelledim böylece runnable (bellow). Birkaç şey tanımlanmamıştı.

require 'nokogiri' 
require 'net/http' 

def fetch(uri_str, limit = 10) 
    # You should choose better exception. 
    raise ArgumentError, 'HTTP redirect too deep' if limit == 0 

    url = URI.parse(URI.encode(uri_str.strip)) 
    puts url 

    #get path 
    headers = {} 
    req = Net::HTTP::Get.new(url.path,headers) 
    #start TCP/IP 
    response = Net::HTTP.start(url.host,url.port) { |http| 
     http.request(req) 
    } 

    case response 
    when Net::HTTPSuccess 
    then #print final redirect to a file 
    puts "this is location" + uri_str 
    puts "this is the host #{url.host}" 
    puts "this is the path #{url.path}" 

    return response 
    # if you get a 302 response 
    when Net::HTTPRedirection 
    then 
    puts "this is redirect" + response['location'] 
    return fetch(response['location'], limit-1) 
    else 
    response.error! 
    end 
end 

response = fetch("http://www.google.com/") 
puts response 
noko = Nokogiri::HTML(response.body) 
puts noko 

senaryo hiçbir hata veriyor ve içerik yazdırır. Aldığınız içerik nedeniyle Nokogiri hatası alıyor olabilirsiniz. Nokogiri ile karşılaştığım yaygın bir sorun, karakter kodlamasıdır. Tam bir hata olmadan neler olduğunu söylemek imkansız. Ben (özellikle this answer) Aşağıdaki StackOverflow Sorular

ruby 1.9: invalid byte sequence in UTF-8 bakarak recommnend ediyorum

How to convert a Net::HTTP response to a certain encoding in Ruby 1.9.1?

+0

Teşekkürler, ama nokogiri hala bana bu hatayı veriyor –

+0

Çok teşekkürler Mr.Simard, Karakter kodlamasını araştıracağım. –

+0

Daha ayrıntılı bir hata ayıklama iletilerini nasıl görebilirim? Nokogiri'nin bana verdiği tek hata bu node_set'in bir Nokogiri :: XML :: Nodeset –