Can't submit web form using urllib2 in python
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?
精彩评论