2014-09-02 39 views
0

我想從使用機械化的網站返回電子郵件地址。通過使用下面的代碼,我很容易確定是否在頁面上找到了「@」符號。使用機械化提取電子郵件地址

但是,我想返回@符號周圍的字符以確定它是否可能是電子郵件地址。任何人都知道,一旦找到了@我可能會返回周圍的角色?

我知道機械化可以返回鏈接,但電子郵件地址可能不是鏈接。謝謝!

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 
+1

你需要使用正則表達式:'電子郵件= page.body [正則表達式]' – pguardiario 2014-09-02 03:03:44

回答

0

建設關什麼pguardario說,因爲你看,你已經可以刮掉頁面您需要的信息以匹配文本主體的模式,這是不是一個真正的機械化相關的問題。

相反,它是一個基於正則表達式:

喜歡的東西

# Naive e-mail match regex, plenty out there to google though this might be enough 
emails = /(\[email protected]+[\w\.]+)/.match page.body.to_s 

emails.each do |email| 
    puts email.to_s 
end 

正則表達式: http://rubular.com/r/PHNhUfyGaC