ruby - URI Extract 在冒号处转义,有什么办法可以避免这种情况?

标签 ruby uri scrape

我在下面有以下函数,通常会吐出一个 URL,例如 path.com/p/12345

有时,当一条推文在推文前包含一个冒号时,例如

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 之前带有冒号的推文,并将该结果从循环中删除,这样它就不会返回到创建的数组中。

最佳答案

您只能选择 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)。

关于ruby - URI Extract 在冒号处转义,有什么办法可以避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9137981/

相关文章:

api - REST - 如何格式化内容中的链接

python beautifulsoup 抓取网站

ruby-on-rails - 命名空间模块和 Rails 3.1.3 autoload_path

ruby - 用 Ruby 创建 COM 服务器?

ruby - 中间人服务器只是在 Windows 8 上用文件观察器停止

ruby-on-rails - 带有位置运算符的 Mongoid 更新不起作用

c# - 如何检查 uri 字符串是否有效

java - 不透明和分层 URI 之间的区别?

php - xpath查询不返回数据

python - 从 udp 和 http torrent tracker 响应获取 IP 地址