Skip to content

Instantly share code, notes, and snippets.

@n3xem
Created July 18, 2018 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n3xem/db198d28372e999b3b631baa19fd6c27 to your computer and use it in GitHub Desktop.
Save n3xem/db198d28372e999b3b631baa19fd6c27 to your computer and use it in GitHub Desktop.
mechanizeを使ってGithubにログインするソースです
#-*- coding:utf-8 -*-
require "open-uri"
require "nokogiri"
require 'mechanize'
# プログラムの実行に必要なgemをインストールしてください
# $gem install nokogiri
# $gem install mechanize
# mechanize が2018/06/25現在、依存ライブラリの作者がプルリクのMergeをサボって上手く動かないので以下の手順でpersistent.rbを書き換えてください
# https://github.com/drbrain/net-http-persistent/pull/90/files
agent = Mechanize.new
agent.get('https://github.com/login') do |page|
loginform = page.forms[0]
#メールアドレスとパスワードを入れておく
loginform.field_with(:name=>"login").value="MAIL_ADDRESS"
loginform.field_with(:name=>"password").value="PASSWORD"
agent.submit(loginform)
#ログインできていなければExit
html = agent.get("https://github.com").content.toutf8
contents = Nokogiri::HTML.parse(html,nil,nil)
if contents.css("ul.user-nav").empty? then
puts "E-mail address or password is wrong."
exit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment