creating a python voting bot

Asked

Viewed 6,679 times

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 : http://soulegal.byethost7.com/wp/2016/07/28/pesquisa-eu-sou-legal

The wordpress plugin is WP-Polls.

It’s my site. I can only vote manually as many times as I want but the code doesn’t work (it doesn’t make any mistakes but it doesn’t vote). Someone helps?

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 (request) was successful (code 200) but we need a browser (browser), or something that processes javascript implicitly (unknown). However good programmers have developed a module called Selenium that makes it all automatically (open the browser, select, submit the form, ect...). And pussui a documentation well-structured:

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))
  • I managed to run the code on Kali Linux. It worked. Thank you very much. All that remains is for me to understand the code.

  • If you want to open a chat and I’ll explain step by step. I’m glad it worked @Edmail

  • If you can, I’d appreciate it!

4

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

In such cases, and if you are using python 3 you can use as an alternative last resort a module that simulates the mouse and keyboard inputs of the computer called 'Pyautogui'.

To install it on linux just do the following on 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 your case, 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)
  • Rui why I use time.sleep(2)?

  • To wait for the user to manually select the browser on the voting page.

  • Thank you for the code. Very useful!

  • @Edmail You’re welcome, always at your beck and call.

3

Check the return of the request. To get it right, the status_code needs to be 200:

response = requests.post(url, data=dados)
print(response)
print(response.status_code)
  • I will check and return! Thank you.

  • The output is <Response [200]> 200

  • print(response.text) returns what?

  • This site requires Javascript to work, Please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

  • Yeah, so follow Miguel’s response, which already has a good roadmap for resolution.

  • Thank you for your contribution!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.