开发者

Need to build a url and work with the returned result

开发者 https://www.devze.com 2023-03-10 12:55 出处:网络
I would like to start with a little script that fetches the examination results of me and my friends from our university website.

I would like to start with a little script that fetches the examination results of me and my friends from our university website.

I would like to pass it the roll number as the post parameter and work with the returned data,开发者_如何转开发 I don't know how to create the post string.

It would be great if someone could tell me where to start, what are the things to learn, links to a tutorial would be most appreciated.

I don´t want someone to write code for me, just guidance on how to get started.


I've written a solution here just as a reference for whatever you might come up with. There are multiple ways of attacking this.

#fetch_scores.rb

require 'open-uri'

#define a constant named URL so if the results URL changes we don't
#need to replace a hardcoded URL everywhere.
URL = "http://www.nitt.edu/prm/ShowResult.html?&param="

#checking the count of arguments passed to the script.
#it is only taking one, so let's show the user how to use
#the script
if ARGV.length != 1
  puts "Usage: fetch_scores.rb student_name"
else
  name = ARGV[0] #could drop the ARGV length check and add a default using ||
  # or   name = ARGV[0] || nikhil
  results = open(URL + name).read
end

You might examine Nokogiri or Hpricot to better parse/format your results. Ruby is an "implicit return" language so if you happened to wonder why we didn't have a return statement that's because results will be returned by the script since it was last executed.


You could have a look at the net/http library, included as part of the standard library. See http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html for details, there are some examples on that page to get you started.


A very simple way to do this is to use the open-uri library and just put the query parameters in the URL query string:

require 'open-uri'
name = 'nikhil'
results = open("http://www.nitt.edu/prm/ShowResult.html?&param=#{name}").read

results now contains the body text fetched from the URL.

If you are looking for something more ambitious, look at net/http and the httparty gem.

0

精彩评论

暂无评论...
验证码 换一张
取 消