2011-11-06 2 views
3

ruby ​​mechanize를 사용하여 Outlook Web Access 계정의 특정 전자 메일에 액세스하려고합니다.Ruby Mechanize Outlook Web Access

다음 코드를 사용하고 있습니다.

 
    require 'mechanize' 
    require 'logger' 

    a = Mechanize.new 
    a.cookie_jar(HTTP::Cookies.new) 
    a.log = Logger.new('log1.log') 

    a.get('htts://webmail.xxxxxxx.org/') do |page| 

     my_page = page.form_with(:action => '/owa/auth.owa') do |f| 
     f.username = "------------" 
     f.password = "------------" 
     end.click_button 

     #a.cookie_jar.load('cookies.yml') 

     a.get('https://webmail.xxxxxxx.org/owa/Inbox/?Cmd=contents&Page=1') do |p| 
      file = File.new("new.xml","w+") 
      file.puts p.parser.to_xml 
      file.close 
     end 

    end 

왜이 코드가 작동하지 않습니까?

+3

질문은 어디 있나요? –

답변

3

이것은 Nokogiri를 사용하는 OWA 검색 스크립트이며, SSL owa 교환 웹 사이트에 기계화합니다.

rubygems, mechanize (+ deps) 및 하이 라인이 설치되어 있어야합니다.

 
    require 'rubygems' 
    require 'mechanize' 
    require 'logger' 
    require 'highline/import' 

    @url = 'https://email.***.***/Exchange' 

    @mechanize = Mechanize.new { |a| a.log = Logger.new('./log1.log') } 

    #In case you dont have trusted certs 
    @mechanize.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

    @mechanize.user_agent_alias = 'Windows Mozilla' 
    @mechanize.keep_alive = 'enable' 

    username = ask("Enter your username: ") 
    domain = ask("Enter your domain: ") 
    password = ask("Enter your password: ") {|q| q.echo = "*" } 

    @mechanize.get(@url) do |page| 

     form = page.forms[0] 

     form["username"] = domain + '\\' + username 
     form["password"] = password 
     page = form.submit 
    end 

    ## Common Mailbox Schemes 
    @mailbox = "#{username}" 

    # @mailbox = "#{username}@#{domain}.#{tld}" 
    # @mailbox = "#{username}@#{domain}" 

    @inbox = @url + "/#{@mailbox}/Inbox/?Cmd=contents&Page=1&View=Unread%20Messages" 

    inboxlisting = @mechanize.get(@inbox) do |page| 

     fragment = Nokogiri::HTML(page.body) 
     ["//img[@src='/exchweb/img/icon-msg-unread.gif']"].each do |xpathq| 
     puts "Found #{fragment.xpath(xpathq).count} new emails." 
     end 

     ["//img[@src='/exchweb/img/icon-mtgreq.gif']"].each do |xpathq| 
      puts "Found #{fragment.xpath(xpathq).count} new meeting requests." 
     end 

    end 

예 스크립트 출력 :

 

    $ ruby ./owa.rb 
    Enter your username: john.doe 
    Enter your domain: mywork 
    Enter your password: ************ 
    Found 31 new emails. 
    Found 3 new meeting requests. 

+0

FYI : MIT 라이센스. 저작권 - shadowbq 2012 – shadowbq

0

나는이 사이트에 액세스하기 위해 Selenium과 Capybara를 사용하는 것이 더 나을지 모른다.

관련 문제