2012-07-05 6 views
5

पार्सिंग HttpResponse साथ HttpResponse पार्स करने, मैं कर रहा हूँ मुसीबत पार्स करने HttpResponse Nokogiri के साथ वस्तुओं।रूबी Nokogiri</p> <p>हाय के साथ 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) 

लाने जब मैं इस एचटीएमएल प्रिंट करना निरर्थक शब्दों की एक पूरी गुच्छा और Nokogiri शिकायत है कि "node_set एक Nokogiri होना चाहिए :: एक्सएमएल :: nodeset

किसी को भी मदद की पेशकश कर सकता है यह काफी सराहना की जाएगी

+1

आप इस गर्म गंदगी के बजाय मशीनीकरण उपयोग करना चाहिए। यह आपके लिए एन्कोडिंग के साथ रीडायरेक्ट और सौदों का ख्याल रखता है। – pguardiario

उत्तर

4

पहली बात। आपका 012,372,विधि Net::HTTPResponse ऑब्जेक्ट देता है न कि केवल शरीर। आपको शरीर को नोकोगिरी प्रदान करना चाहिए।

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

मैंने आपकी स्क्रिप्ट को अपडेट किया है, इसलिए यह चलने योग्य (भाई) है। कुछ चीजें अपरिभाषित थीं।

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 

स्क्रिप्ट में कोई त्रुटि नहीं है और सामग्री प्रिंट करता है। आपको प्राप्त होने वाली सामग्री के कारण आपको नोकोगिरी त्रुटि मिल रही है। नोकोगिरी के साथ मुझे सामना करने वाली एक आम समस्या चरित्र एन्कोडिंग है। सटीक त्रुटि के बिना यह बताना असंभव है कि क्या हो रहा है।

मैं निम्नलिखित StackOverflow सवाल

ruby 1.9: invalid byte sequence in UTF-8 में (विशेष रूप से this answer) देख recommnend था

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

+0

धन्यवाद, लेकिन nokogiri अभी भी मुझे यह त्रुटि देता है –

+0

बहुत धन्यवाद श्री सिमार्ड, मैं चरित्र एन्कोडिंग देखता हूं। –

+0

मैं एक और वर्बोज डीबग संदेश कैसे देख सकता हूं? नोकोगिरी मुझे एकमात्र त्रुटि दे रही है यह है कि यह नोड_सेट एक नोकोगिरी :: एक्सएमएल :: नोडसेट –

संबंधित मुद्दे