URI 提取在冒号处转义,有什么方法可以避免这种情况吗?

问题描述 投票:0回答:1

我有以下函数,通常会输出一个 URL,例如

path.com/p/12345

有时,当推文在推文之前包含冒号时,例如

RT:有些东西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 之前带有冒号的任何推文,并将该结果从循环中删除,以便它不会返回到创建的数组。

ruby web-scraping uri
1个回答
3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.