-
-
Save vinayakkulkarni/7adbcb69bb9ce84c08285e0dc802ddba to your computer and use it in GitHub Desktop.
Requests and Beautiful Soup example, following the form of http://bpaste.net/show/kMetvCdrfnzh5RgiUKU4/
This file contains hidden or 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
from BeautifulSoup import BeautifulSoup | |
import requests | |
import urlparse | |
URL = 'example.com' | |
s = requests.Session() | |
def fetch(url, data=None): | |
if data is None: | |
return s.get(url).content | |
else: | |
return s.post(url, data=data).content | |
soup = BeautifulSoup(fetch(URL)) | |
form = soup.find('form') | |
fields = form.findAll('input') | |
formdata = dict( (field.get('name'), field.get('value')) for field in fields) | |
formdata['username'] = u'username' | |
formdata['password'] = u'password' | |
print formdata | |
posturl = urlparse.urljoin(URL, form['action']) | |
print posturl | |
r = s.post(posturl, data=formdata) | |
print r.text | |
print s.get(URL).text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment