Skip to content

Instantly share code, notes, and snippets.

@romichi
Created May 13, 2013 11:12
Show Gist options
  • Save romichi/5567611 to your computer and use it in GitHub Desktop.
Save romichi/5567611 to your computer and use it in GitHub Desktop.
pythonでyahooにログインする
# -*- coding:utf-8 -*-
import time
import mechanize
def yahooLogin(username, password):
print u"ログイン開始"
#mechanizeの設定
br = mechanize.Browser()
br.set_handle_robots(False) # robotsに従わない
br.addheaders = [("User-agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")] # UserAgentの設定
# Yahooのログインページに行く
br.open("http://www.yahoo.co.jp/") # クッキー用
html = br.open("https://login.yahoo.co.jp/config/login?.src=www&.done=http://www.yahoo.co.jp")
# .albatrossの値の取得
for line in html:
if line.find('document.getElementsByName(".albatross")[0].value') != -1:
albatross = line[53:-3]
print "albatross:", albatross
br.select_form(nr=0); # フォームの選択
br.form.controls[1].readonly = False # .albatrossをreadonlyじゃなくす
# フォームの入力
br["login"] = username
br["passwd"] = password
br[".albatross"] = albatross
time.sleep(5)
br.submit(); # 送信
enc = br.encoding() # encodeの取得
print br.title().decode(enc) # タイトルの表示
print u"ログイン終了"
if __name__ == "__main__":
userName = ""
password = ""
yahooLogin(userName, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment