Shell: Connecting to a website and accessing a field

后端 未结 4 1369
面向向阳花
面向向阳花 2021-01-03 02:39

I want to write a script that takes an argument which is text, opens a connection to a specific website and input the arg into text field using the field\'s ID. Is this poss

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 03:16

    If you know exactly which field you need to fill, then this can be done using lynx. Assume you get the string S with your script as an input argument. Then you create a command script, which will guide lynx through its behaviour.

    For example, suppose S=foo, and your field is the second field in the web page. After that, there are two more fields, and then the submit button. After that you wait for the page to load and click the hyperlink (after that you exit). The web page is www.something.com.

    The command script would be in a file bar.txt:

    key  //get to first field
    key  //get to second field
    key f     //input f
    key o     //input o
    key o     //input o
    key  //get to third field
    key  //get to fourth field
    key  //get to sumbit button
    key ^J    //click submit and wait for load
    key  //get to hyperlink
    key ^J    //click hyperlink and wait for load
    key Q     //exit
    key y     //confirm exit
    

    The main command would then be lynx www.something.com -accept_all_cookies -cmd_script=bar.txt

    Now all you need to do is dynamically create the input string.

    #!/bin/bash
    script=bar.txt
    input=$1
    webpage=www.something.com
    len=${#input}
    echo 'key ' > $script
    echo 'key ' >> $script
    for i in `echo $input|fold -w1` 
    do
        echo 'key '$i >> $script
    done
    echo 'key ' >> $script
    echo 'key ' >> $script
    echo 'key ' >> $script
    echo 'key ^J' >> $script
    echo 'key ' >> $script
    echo 'key ^J' >> $script
    echo 'key Q' >> $script
    echo 'key y' >> $script
    
    lnyx $webpage -accept_all_cookies -cmd_script=bar.txt
    

    Now all you need to do is save the script, modify it to be executable and call it ./script your_string

提交回复
热议问题