creating a voting bot in python

3
import requests

dados = {"action": "polls",
         "view":"process",
         "poll_id":"2",
         "poll_2":"6",
          "poll_2_nonce":"e29cc82a53"}

url = "http://soulegal.byethost7.com/wp/wp-admin/admin-ajax.php"

requests.post(url, data=dados)

URL: link

The wordpress plugin is WP-Polls.

The site is mine. I can only manually vote as many times as I want but the code does not work (it gives no error but does not vote). Does anyone help?

    
asked by anonymous 29.07.2016 / 05:12

3 answers

6

With requests will not give because it does not process Javascript, if you do:

import requests

req = requests.get("http://soulegal.byethost7.com/wp/2016/07/28/pesquisa-eu-sou-legal/")
print(req.status_code)
print(req.text)

You can see what you print:

  

200

     

... This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support ...

This basically tells us that the request was successful (code 200) but that we need a browser, or something that processes javascript implicitly (I do not know). However good programmers have developed a module called Selenium that does it all automatically (open browser, select, submit the form, ect ...). And a well structured documentation :

To vote on several options:

from selenium import webdriver

driver = webdriver.Firefox()
opts = ['poll-answer-6', 'poll-answer-7', 'poll-answer-8'] # id das opcoes em que deseja votar

for i in opts:
    driver.get("http://soulegal.byethost7.com/wp/2016/07/28/pesquisa-eu-sou-legal/?ckattempt=1")
    opt = driver.find_element_by_id(i) # selecionamos o elemento com id
    opt.click()
    btn_submit = driver.find_element_by_name('vote') # selecionamos o elemento que como name tem 'vote', o nosso botao
    btn_submit.click()
    print('Votou em {}'.format(driver.find_element_by_css_selector('label[for="' +i+ '"]').text))

To select the same option several times, in this case 5 times:

from selenium import webdriver

driver = webdriver.Firefox()
opt = 'poll-answer-7' # id opcao em que deseja votar
votos = 5

for i in range(votos):
    driver.get("http://soulegal.byethost7.com/wp/2016/07/28/pesquisa-eu-sou-legal/?ckattempt=1")
    opt_ele = driver.find_element_by_id(opt)
    opt_ele.click()
    btn_submit = driver.find_element_by_name('vote')
    btn_submit.click()

opt_text = driver.find_element_by_css_selector('label[for="' +opt+ '"]').text
print('Votou em {} {} vezes'.format(opt_text, votos))
    
29.07.2016 / 13:04
3

Check the return of the request. To be successful, status_code must be 200:

response = requests.post(url, data=dados)
print(response)
print(response.status_code)
    
29.07.2016 / 06:18
3

There are rare exceptions where it is not possible to fill fields in a form using a browser simulator such as selenium.

In these cases, if you are using python 3 you can use as a last resort alternative a module that simulates the computer mouse and keyboard entries called 'PyAutoGUI '.

To install it in linux just do the following at the command line:

pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
pip3 install pyautogui

An example of its use, for you, would be something like this:

import pyautogui, time
time.sleep(2)

# funcao para ir, clicar e esperar
def clicar(link, tempo_mov=0.5):
    area = pyautogui.locateOnScreen(link)
    centro_area = pyautogui.center(area)
    pyautogui.moveTo(centro_area, duration=tempo_mov)
    pyautogui.click(centro_area)

while True:
    clicar('./opcao.png')
    clicar('./botao_voto.png')
    clicar('./refescar.png')
    time.sleep(5)
    
29.07.2016 / 22:53