我想从一个小脚本开始,它从我们的university website获取我和我朋友的考试结果。
我想将卷数作为post参数传递给它,并处理返回的数据,我不知道如何创建post字符串。
如果有人能告诉我从哪里开始,学到什么,到教程的链接,那就太好了。
我不希望有人为我写代码,只是指导我如何入门。
发布于 2011-06-07 03:13:54
我在这里写了一个解决方案,只是作为你可能想出的任何东西的参考。有多种方法可以解决这个问题。
#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?¶m="
#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
您可以检查Nokogiri或Hpricot以更好地解析/格式化结果。Ruby是一种“隐式返回”语言,所以如果你想知道为什么我们没有返回语句,那是因为结果将由脚本自上次执行以来返回。
发布于 2011-06-06 22:54:41
有关详细信息,请参阅http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html,该页面上有一些示例可以帮助您入门。
发布于 2011-06-06 22:55:33
一种非常简单的方法是使用open-uri
库,并将查询参数放入URL查询字符串中:
require 'open-uri'
name = 'nikhil'
results = open("http://www.nitt.edu/prm/ShowResult.html?¶m=#{name}").read
results
现在包含从URL获取的正文文本。
如果你正在寻找更有野心的东西,看看net/http
和httparty
gem。
https://stackoverflow.com/questions/6253101
复制相似问题