Created
March 31, 2013 16:28
-
-
Save Lukasa/5281176 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 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 |
This is a good snippet. Can it be updated to capture other types of inputs, such as "select" inputs?
@david-drinn Maybe this would help: https://gist.github.com/rgov/ea3151f016aa2c2ce4b0893b07210f11
Thanks! This is helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Olá!
Recentemente testei seu código para um projeto. Gostaria de aproveitar a oportunidade e sugerir modificações em função do uso de Python3.
`#Python3
from bs4 import BeautifulSoup
import requests
from urllib.parse import urljoin
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 = urljoin(URL, form['action'])
print (posturl)
r = s.post(posturl, data=formdata)
print (r.text)
print (s.get(URL).text())`