How to input values and click button with Requests

2019-09-07 00:12发布

问题:

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 there is a download button. Now i want to go throught the process with my python script.

so far i have this:

def download_song(song_name):
    import requests

    with requests.Session() as c:
        url = r"http://www.youtube-mp3.org/"
        c.get(url)

it barely anything... i have tried to check the documentation on there website. i dont think i need any authentication for any of this. previously i have used requests with bs4 to find links and information on a pages, never tried to put input.

thanks for any help

回答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