开发者

Can't submit web form using urllib2 in python

开发者 https://www.devze.com 2023-04-04 18:41 出处:网络
I am trying to submit the form at http://www.harshtechtalk.com/contact-us-harsh-tech-talk using the following code but no success. Please help!

I am trying to submit the form at http://www.harshtechtalk.com/contact-us-harsh-tech-talk using the following code but no success. Please help!

#!C:/Python27/python.exe

import urllib
import urllib2

def main():
        pro开发者_如何转开发xy_info={
                        'user' : 'abc@abc.com',
                        'pass' : 'xyz',
                        'host' : 'xxxxxxxx',
                        'port' : 80
                        }
        proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)s@%(host)s:%(port)d"
% proxy_info})
        opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
        urllib2.install_opener(opener)
        url = 'http://www.harshtechtalk.com/contact-us-harsh-tech-talk'
        values = {
                        'your-name':'test_name',
                        'your-email':'test@gmail.com',
                        'your-subject':'python_test',
                        'your-message':'test_message'
                        }
        data = urllib.urlencode(values)
        req = urllib2.Request(url,data)
        response = urllib2.urlopen(req)
        the_page = response.read()
        print "done"

if __name__ == '__main__':
        main()

PS - I don't want to use "mechanize" because it doesn't support javascript. Please let me know if there is any other module which I can use to handle dynamic web forms.


Handling AJAX web forms generally requires using selenium so that Python is driving a real web browser with a JavaScript engine installed. Some people also talk about phantomjs but I am not aware of an official Python module for it at this point. See also:

Headless, scriptable Firefox/Webkit on linux?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号