python使用urllib和httplib库模拟提交表单。
比如登录[login.html]界面是
<form action="" method="POST">
<input name="user"/>
<input name="passwd"/>
</form>
python模拟login登录的时候可以作如下代码:
import
urllib
, httplib
url = 'localhostl'
path = '/login.html'
headers = {
"Accept"
:"text/html"
,"User-Agent"
:"ShopEx_SAAS"
,"Content-type"
:"application/x-www-form-urlencoded"
}
conn = httplib
.HTTPConnection
(
url)
params = {
'user'
:'tito'
,'passwd'
:'tito'
}
try
:
conn.request
(
"POST"
,path,params,headers)
r=conn.getresponse
(
)
if
r.status
is
not
200
:
print
'download goods error'
sys
.exit
(
)
except
urllib2
.URLError
,IOError
:
print
URLError
sys
.exit
(
)
print
r.read
(
)