Created
July 5, 2013 06:10
-
-
Save tazjel/5932299 to your computer and use it in GitHub Desktop.
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
import urllib | |
import urllib2 | |
from cookielib import CookieJar | |
from BeautifulSoup import BeautifulSoup | |
import re | |
""" | |
watch -d -n 5 'python 4irik.py' | |
""" | |
cookies = CookieJar() | |
def scrap_params(): | |
site = 'http://kvnkpi.org.ua/apoll/8-2011-04-13-18-50-19.html' | |
request = urllib2.Request(site) | |
response = urllib2.urlopen(request) | |
cookies.extract_cookies(response, request) | |
params = {} | |
soup = BeautifulSoup(response) | |
form = soup.find('form',attrs={'name': 'apoll_form2'}) | |
input_params = form.findAll('input', attrs={'type': 'hidden'}) | |
for input_param in input_params: | |
params[input_param.attrs[1][1]]=input_param.attrs[2][1] | |
params['voteid'] = '46' | |
params['task_button'] = 'Vote' | |
return params | |
def votesCount(page): | |
soup = BeautifulSoup(page) | |
for result in soup.findAll('div', attrs={'style': 'padding:3px;'}): | |
print result.string | |
print re.sub(r"\s*([0-9]*).*", r"\1",soup.findAll('td', attrs={'class': 'smalldark'})[1].string) | |
def send_request(): | |
url = 'http://kvnkpi.org.ua/apoll.html' | |
data = urllib.urlencode(scrap_params()) | |
req = urllib2.Request(url, data) | |
cookie_handler= urllib2.HTTPCookieProcessor( cookies ) | |
redirect_handler= urllib2.HTTPRedirectHandler() | |
opener = urllib2.build_opener(redirect_handler,cookie_handler) | |
votesCount(opener.open(req)) | |
if __name__ == '__main__': | |
send_request() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment