Pythonã§HTTPSéä¿¡ãã¦ã¯ã¦ãªã«ãã°ã¤ã³ãã¦ã¿ã
ä»æ¸ãã¦ããããã°ã©ã ã§httpséä¿¡ã使ã£ããã°ã¤ã³å¦çã£ã½ãäºãããå¿
è¦ãåºã¦ããã®ã§Pythonã®ããã¥ã¢ã«ãèªãã§ãç°¡åã«ã§ãããã¨ããããã¾ãããurllib2ã使ãã¿ããã ã
http://www.python.jp/doc/2.5/lib/urllib2-examples.html
ãµã³ãã«ã¨ãã¦ãã¯ã¦ãªã«ãã°ã¤ã³ããããã°ã©ã ãæ¸ãã¾ããã
import urllib2 url = 'https://www.hatena.ne.jp/login' name = '' # type your own username password = '' # password req = urllib2.Request(url, 'name=%s&password=%s' % (name, password)) #req.add_header('hoge', 'fuga') cont = urllib2.urlopen(req).read() f = open('out.html', 'w') f.write(cont) f.close()
å¤æ°ã«ã¦ã¼ã¶åã¨ãã¹ã¯ã¼ããå
¥åãã¦å®è¡ããã¨out.htmlã«ã¬ã¹ãã³ã¹ãä¿åããã¾ããå®è¡å¾ã«firefoxã§éãã¨ã
ãã°ã¤ã³ã§ãã¾ããã
ãã ããã ã¨ã¬ã¹ãã³ã¹ã«å«ã¾ããã¯ããã¼æ
å ±ãä¿æãã¦ãããªãã®ã§ã»ãã·ã§ã³ãç¶æã§ãããã°ã¤ã³å¦çãããæå³ãããã¾ãããã¯ããã¼ãæ±ãå¿
è¦ãããã¾ãã
http://www.python.jp/doc/2.5/lib/module-urllib2.html
http://www.python.jp/doc/2.5/lib/http-cookie-processor.html
urllib2.build_openerã§ã¤ã³ã¹ã¿ã³ã¹åãããOpenerDirectorã¯ã©ã¹ã¯ãå é¨ã§ãã³ãã©ç¾¤ãæã£ã¦ãã¦ããªã¯ã¨ã¹ãã»ã¬ã¹ãã³ã¹ããã®ãã³ãã©ç¾¤ã«å¦çãããã»ã»ã»ããããé©å½ãªäºè¨ã£ã¦ã¾ãã
urllib2ã«ã¯ãã³ãã©ã®ä¸ã¤ã¨ãã¦ãHTTPCookieProcessorã¨ããHTTPãªã¯ã¨ã¹ãã«å¯¾ãã¦Cookieããããä»å ããããã¬ã¹ãã³ã¹ã«å«ã¾ããSet-Cookieããããèªã¿åã£ã¦ä¿åãã¦ãããCookieå¦çã®ãã³ãã©ãå®ç¾©ããã¦ãã¾ãããããã©ã«ãã®ãã³ãã©ã¨ãã¦ã¯å®ç¾©ããã¦ãã¾ããããã®ããèªåã§ãã³ãã©ç¾¤ã«å ããå¿ è¦ãããã¾ããOpenerDirector.add_handlerã§ãã³ãã©ã追å ãã¾ãã
試ãã«ããã°ã¤ã³å¾ã«ã¯ã¦ãªãã¤ã¢ãªã¼ã®ç·¨éãã¼ã¸ã«ã¢ã¯ã»ã¹ãã¾ããããã¤ã¢ãªã¼ã®ç·¨éãã¼ã¸ã¯ãã°ã¤ã³ãã¦ããªãã¨ããã°ãããã«ãªãã¤ã¬ã¯ãããã¦ãã¾ãã¾ããã¯ããã¼ãæ£ããå¦çããã¦ããã°ç·¨éãã¼ã¸ã表示ãããã¯ãã§ãã
import urllib2 import cookielib name = '' password = '' url = 'https://www.hatena.ne.jp/login' req = urllib2.Request(url, 'name=%s&password=%s' % (name, password)) #req.add_header('hoge', 'fuga') opener = urllib2.build_opener() opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) conn = opener.open(req) url = 'http://d.hatena.ne.jp/%s/edit' % name req = urllib2.Request(url) conn = opener.open(req) cont = conn.read() f = open('out.html', 'w') f.write(cont) f.close()
ãã°ã¤ã³ã§ãã¾ããã
é¢é£ã¨ã³ããª
- iPhone Mobile Safariã§GPSãã©ããã³ã°ããã話
- å¹³æ¹æ°ã®å¤å®ãé«éã«ãããã
- pythonåãHTTPé¢é£ã¢ã¸ã¥ã¼ã«ã®requestsã便å©ããã
- pythonããgdataã©ã¤ãã©ãªã§Google Calendarãæä½ããéã«302 Redirect RequestError: Moved Temporarilyãçºçããåé¡
- ãã¼ãã®å¤å