Skip to content

Instantly share code, notes, and snippets.

@nickrobson
Last active May 20, 2016 13:27
Show Gist options
  • Save nickrobson/ece34f636d51b6c8d1a08eab0d0d0f03 to your computer and use it in GitHub Desktop.
Save nickrobson/ece34f636d51b6c8d1a08eab0d0d0f03 to your computer and use it in GitHub Desktop.
Automatic Strawpoll voter
import sys
import time
import urllib
import urllib2
import bs4
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'}
def scrape(id):
req = urllib2.Request('http://www.strawpoll.me/%s' % id, headers=headers)
content = urllib2.urlopen(req).read()
soup = bs4.BeautifulSoup(content, 'html5lib')
sectoken = soup.find('form').find('input', id='field-security-token')['value']
authtoken = soup.find('form').find('input', id='field-authenticity-token')['name']
opts = soup.find('div', id='field-options').find_all('div')
optz = []
for i in range(len(opts)):
opt = opts[i]
name = opt.find('label').get_text()
value = opt.find('input')['value']
optz.append({'name': name, 'value': value})
print '%d:' % (i+1), name, '(%s)' % value
opt = optz[int(raw_input('Choice: ')) - 1]
form_vals={'security-token':sectoken, authtoken:'', 'options':opt['value']}
form_vals = urllib.urlencode(form_vals)
while True:
req = urllib2.Request('http://www.strawpoll.me/%s' % id, data=form_vals, headers=headers)
urllib2.urlopen(req)
time.sleep(0.01)
if __name__ == '__main__':
if len(sys.argv[1:]):
scrape(sys.argv[1])
else:
print 'Usage: python %s [strawpoll_id]' % sys.argv[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment