我积极解决 发表于 2017-4-22 10:03:10

python 模拟POST

  
python使用urllib和httplib库模拟提交表单。
比如登录界面是

<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
(
)






页: [1]
查看完整版本: python 模拟POST