Skip to content Skip to sidebar Skip to footer

How To Input Values And Click Button With Requests?

With the requests module i eventually want to download a song. if you head to youtube-mp3.org, there is one input bar and one convert button. Shortly after the convert is finished

Solution 1:

concerning the 'download' link (easy)

Just make a request to the url

concerning the form (more complicated)

What the browser does

When you click on a 'submit' button inside a <form> element, the browser collects all data from all the <input> elements inside the form, in this case it collects content of the text input where you put the url of the video. The browser then sends a request to the URL specified in the form's 'action' attribute with the method specified in the form's 'method' attribute .

The form in question:

<form method="get" action="/" id="submit-form">
    <input id="youtube-url" value="http://www.youtube.com/watch?v=KMU0tzLwhbE" onclick="sAll(this)" autocomplete="off" type="text">
    <div id="btns">
        <input id="submit" value="Convert Video" type="submit">
    </div>
</form>

What you have to do

You have to make a request as if you were a browser. You have the URL of the video you want to convert, now you have to create an HTTP request to the URL specified in the form (in this case '/' - I think that's relative to the current URL of the page but I'm not sure) with the method specified in the form (in this case GET). See the doc on how to send form-encoded data

Your request to convert a video should look something like this:

page_url = 'http://www.youtube-mp3.org/'
video_url = "http://www.youtube.com/watch?v=KMU0tzLwhbE"
resp = requests.get(page_url, data={'youtube-url': video_url})

EDIT: Extra

In the 'developers tools' of your browser you can capture the HTTP request that your browser sends when you click on 'Convert video', that could help


Post a Comment for "How To Input Values And Click Button With Requests?"