Python/mechanize

Top / Python / mechanize

󥹥ȡ

  1. easy_install Υ󥹥ȡ
  2. mechanize Υ󥹥ȡ

easy_install Υ󥹥ȡ

  1. ez_setup.py (http://peak.telecommunity.com/dist/ez_setup.py)
  2. ޥɥץץȤʲ¹
    rem ez_setup.py Υǥ쥯ȥ˰ư
    cd c:\hoge
    
    rem 󥹥ȡ
    python ez_setup.py
    
  3. Scripts եǤƤ󥹥ȡ뤵
    • ̾ Python Υ󥹥ȡǥ쥯ȥ
    • ѥ̤Ƥ̤ʤƤɤǤ

mechanize Υ󥹥ȡ

  1. ޥɥץץȤʲ¹
    rem Scripts ˥ѥ̤äƤʤ
    cd c:\python26\Scripts
    
    rem 󥹥ȡ
    easy_install mechanize
    

ɤΥҥ

ŪʻȤ

# -*- encoding: utf-8 -*-
import mechanize

# 󥹥󥹺
br = mechanize.Browser()

# ܥåȲ
br.set_handle_robots(False)

# URLΥץ
br.open('http://www.hoge.com')

# form: ֺǽformʤ0
br.select_form(nr=0)

# name="id"name="pass" 줿 input ΰˤ줾
br["id"] = "***"
br["pass"] = "***"

# 
br.submit()

# URLΥץ
res = br.open('http://www.hoge.com/login.html')

# HTMLɽ
print res.read()

ҥ

# -*- encoding: utf-8 -*-
import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
br.open('http://www.hoge.com')

# 󥳡ǥ󥰤μ
enc = br.encoding()

# 쥹ݥ󥹤μread() Ȥ
response = br.response()
print response

# 󥯾μ
link = br.links()

# ߤΥڡˤ󥯤ɽ
for link in br.links():
  print link

# 󥯤
br.find_link(text_regex = u"ʸ".encode("utf-8"))

# 󥯤ư
br.follow_link(text_regex = u"ʸ".encode("utf-8"))
br.follow_link(url="URL")

# եμ
form = br.forms()

# ߤΥڡˤեɽ
for form in br.forms():
  print form

# URL إ
br.open("url", data)

# User-Agent 򤤤
br.addheaders = [('User-agent', 'Mozilla/5.0')]

# BASIC ǧ
br.add_password('http://www.hoge.com/', 'id', 'pwd')

# type=textarea  value Ȥ
text = form.find_control(type='textarea').attrs['value']

# ꡼ɥ°
for form in br.forms():
  form.find_control(name="mode").readonly = False
  form.find_control(name="submit").readonly = False


Last-modified: 2011-10-12 () 20:34:25 (2108d)