2012-07-05 29 views
5

Phân tích cú pháp HTTP phản hồi với NokogiriRuby phân tích cú pháp HTTPresponse với Nokogiri

Xin chào, tôi đang gặp sự cố khi phân tích cú pháp đối tượng HTTPsponse với Nokogiri.

tôi sử dụng chức năng này để lấy một trang web ở đây:

lấy một liên kết

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) 

Khi tôi làm html này in một bó toàn bộ gibberish và Nokogiri than phiền rằng "node_set phải là một Nokogiri :: XML :: NOdeset

Nếu bất cứ ai có thể cung cấp trợ giúp, nó sẽ được đánh giá khá cao

+1

Bạn nên sử dụng cơ giới hóa thay vì mớ hỗn độn nóng này. Nó sẽ chăm sóc chuyển hướng và giao dịch với mã hóa cho bạn. – pguardiario

Trả lời

4

Điều đầu tiên của bạnPhương pháptrả về một đối tượng Net::HTTPResponse và không chỉ cơ thể. Bạn nên cung cấp cơ thể cho Nokogiri.

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

Tôi đã cập nhật tập lệnh của bạn để nó có thể chạy được (dưới đây). Một vài điều không được xác định.

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 

Tập lệnh không có lỗi và in nội dung. Bạn có thể gặp lỗi Nokogiri do nội dung bạn nhận được. Một vấn đề phổ biến mà tôi gặp phải với Nokogiri là mã hóa ký tự. Không có lỗi chính xác thì không thể nói chuyện gì đang xảy ra.

Tôi muốn recommnend nhìn vào câu hỏi StackOverflow sau

ruby 1.9: invalid byte sequence in UTF-8 (đặc biệt this answer)

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

+0

Cảm ơn, nhưng nokogiri vẫn cho tôi lỗi này –

+0

Cảm ơn rất nhiều Mr.Simard, tôi sẽ tra cứu mã hóa ký tự. –

+0

Tôi làm cách nào để xem thông báo gỡ lỗi chi tiết hơn? Lỗi duy nhất mà Nokogiri đưa cho tôi là node_set này phải là một Nokogiri :: XML :: Nodeset –

Các vấn đề liên quan