我正在尝试返回的电子邮件地址从一个网站使用机械。通过使用下面的代码,我可以很容易地确定页面上是否有"@“符号。
但是,我想返回@符号周围的字符,以确定它是否可能是电子邮件地址。有人知道一旦找到@,我将如何返回周围的字符吗?
我知道mechanize可以返回链接,但电子邮件地址可能不是链接。谢谢!
require 'mechanize'
mechanize = Mechanize.new { |agent|
agent.open_timeout = 4
agent.read_timeout = 4
agent.max_history = 0
agent.follow_meta_refresh = true
agent.keep_alive = false
}
website = ARGV[0]
keyword = "@"
page = mechanize.get(website)
if page.body.include?(keyword)
puts "found \"#{keyword}\" on #{website}"
else
puts "not found"
end发布于 2014-09-03 21:54:18
根据pguardario所说,由于您希望匹配文本正文中的模式,因此这并不是一个真正与机械化相关的问题,因为您已经可以抓取页面以获取所需的信息。
相反,它是基于regex的:
就像这样
# Naive e-mail match regex, plenty out there to google though this might be enough
emails = /(\w+@+[\w\.]+)/.match page.body.to_s
emails.each do |email|
puts email.to_s
end正则表达式:http://rubular.com/r/PHNhUfyGaC
https://stackoverflow.com/questions/25614827
复制相似问题