Created
November 16, 2012 17:15
-
-
Save c0ldlimit/4089107 to your computer and use it in GitHub Desktop.
Python: Submitting HTTP Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.blog.pythonlibrary.org/2012/06/08/python-101-how-to-submit-a-web-form/ | |
# with urllib | |
import urllib | |
import urllib2 | |
import webbrowser | |
url = "http://duckduckgo.com/html" | |
data = urllib.urlencode({'q': 'Python'}) | |
results = urllib2.urlopen(url, data) | |
with open("results.html", "w") as f: | |
f.write(results.read()) | |
webbrowser.open("results.html") | |
# with requests | |
import requests | |
url = "http://duckduckgo.com/html" | |
payload = {'q':'python'} | |
r = requests.post(url, payload) | |
with open("requests_results.html", "w") as f: | |
f.write(r.content) | |
# http://www.voidspace.org.uk/python/articles/urllib2.shtml | |
# post request | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment