Created
July 18, 2018 01:57
-
-
Save n3xem/db198d28372e999b3b631baa19fd6c27 to your computer and use it in GitHub Desktop.
mechanizeを使ってGithubにログインするソースです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- 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