Python urllib,urllib2 fill form

ⅰ亾dé卋堺 提交于 2019-12-05 19:25:33

This is really more of something you would do with Selenium or similar. selenium.webdriver.common.action_chains.ActionChains.click, perhaps.

IF you look at your forms action attribute you could figure out what uri your form is submitting to. You can then make a post request to that uri. This can be done like :

import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

https://docs.python.org/2/howto/urllib2.html

You could also use the requests library which makes it a lot easier. You can read about it here

Another thing you might need to factor in is the CSRF(Cross Site Request Forgery) token which is embedded in forms. You will have to some how acquire it and pass it in with your post.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!