Ruby URI提取在冒号处转义,有没有办法避免这种情况?
我有下面的函数,它通常会弹出一个URL,比如Ruby URI提取在冒号处转义,有没有办法避免这种情况?,ruby,uri,scrape,Ruby,Uri,Scrape,我有下面的函数,它通常会弹出一个URL,比如path.com/p/12345 有时,当tweet在tweet之前包含冒号时,例如 RT:Something path.com/p/123 该函数将返回: personName: path.com/p/12345 我的职能: $a = 10 def grabTweets() tweet = Twitter.search("[pic] "+" path.com/p/", :rpp => $a, :result_type => "re
path.com/p/12345
有时,当tweet在tweet之前包含冒号时,例如
RT:Something path.com/p/123
该函数将返回:
personName:
path.com/p/12345
我的职能:
$a = 10
def grabTweets()
tweet = Twitter.search("[pic] "+" path.com/p/", :rpp => $a, :result_type => "recent").map do |status|
tweet = "#{status.text}" #class = string
urls = URI::extract(tweet) #returns an array of strings
end
end
我的目标是找到URL前面带有冒号的任何tweet,并从循环中删除该结果,以便它不会返回到创建的数组。您只能选择HTTP URL:
URI.extract("RT: Something http://path.com/p/123")
# => ["RT:", "http://path.com/p/123"]
URI.extract("RT: Something http://path.com/p/123", "http")
# => ["http://path.com/p/123"]
你的方法也可以被清理很多,你有很多多余的局部变量:
def grabTweets
Twitter.search("[pic] "+" path.com/p/", :rpp => $a, :result_type => "recent").map do |status|
URI.extract(status.text, "http")
end
end
我还强烈反对使用全局变量(
$a
)。为了澄清,在url提取后添加“,”http“。这将验证它提取的url在添加到数组之前是否包含http。这是正确的吗?是的,尽管它会检查您正在传递的是“scheme”,所以不仅仅是它在URI中的任何地方。您还可以向它传递一系列协议,例如[“http”,“ftp”]
以包括多个方案。我通常会说,你可以阅读更多,但它的简短令人沮丧。