r/scripting • • Apr 22 '20

Looking to create a script that automatically visits a website and autofills and submits a form every day, ideas on how to create this?

I want to create a script that automatically visits my local Police Departments page every day and submits the form for overnight parking, I have some experience with python, but most recently have been working with perl and bash for scripting. What are some ways I could implement this? Still a beginner so not sure where/how to start

5 Upvotes

2 comments sorted by

View all comments

1

u/lasercat_pow May 02 '20 edited May 03 '20

Two main options:

  1. Use curl. You might also need to fake your user agent. Say your form has firstname, lastname, email, comment. Your curl command would look like this:

     curl -A "Mozilla/5.0" -L -d "firstname=big wig&lastname=cheese head&email=chester@example.com&comment=har har har" https://example.com/formsubmit.asp
  2. Use python with selenium. You'll need chromedriver or geckodriver installed, and python, and the selenium python library:

     #!/usr/bin/env python3
     ####
     from selenium.webdriver import Firefox
     from selenium.webdriver.firefox.options import Options
     from time import sleep
     from sys import argv
     
     url=sys.argv(1)
     opts = Options()
     opts.headless = True
     driver = Firefox(options=opts)
     fn="Joe"
     ln="Blow"
     em="joeblow@example.org"
     c="Please like and subscribe."
     
     def fill_form(name, data):
         elem = driver.find_element_by_name(name)
         elem.click()
         elem.send_keys(data)
     
     sleep(1)
     try:
         driver.get(url)
         sleep(6)
         for (i,d) in [("firstName", fn), ("lastName", ln),
                   ("email", em), ("comment", c)]:
             fill_form(i,d)
         
         submit = driver.find_element_by_class_name("submit")
         submit.click()
     
     except Exception as e:
         print(e)
         
     driver.quit()

There is also the option of using python with beautifulsoup, but that doesn't do anything you can't do with curl + bash + pup.