Getting webpage content with Ruby — I'm having troubles

后端 未结 3 1255
一生所求
一生所求 2020-12-14 21:53

I want to get the content off this* page. Everything I\'ve looked up gives the solution of parsing CSS elements; but, that page has none.

Here\'s the only code that

相关标签:
3条回答
  • 2020-12-14 22:35

    The appropriate way to fetch the content of a website is through the NET::HTTP module in Ruby:

    require 'uri'
    require 'net/http'
    url = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
    r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
    

    File.open() does not support URIs.

    Best wishes,
    Fabian

    0 讨论(0)
  • 2020-12-14 22:36

    You really want to use open() provided by the Kernel class which can read from URIs you just need to require the OpenURI library first:

    require 'open-uri'
    

    Used like so:

    require 'open-uri'
    file = open('http://hiscore.runescape.com/index_lite.ws?player=zezima')
    contents = file.read
    puts contents
    

    This related SO thread covers the same question:

    Open an IO stream from a local file or url

    0 讨论(0)
  • 2020-12-14 22:54

    Please use open-uri, its support both uri and local files

    require 'open-uri'
    contents  = open('http://www.google.com') {|f| f.read }
    
    0 讨论(0)
提交回复
热议问题