如何输入值并单击带有请求的按钮? [英] How to input values and click button with Requests?

查看:88
本文介绍了如何输入值并单击带有请求的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用请求模块,我最终想下载一首歌曲.如果您前往youtube-mp3.org,则有一个输入栏和一个转换按钮.转换完成后不久,就有一个下载按钮.现在,我想使用我的python脚本来完成整个过程.

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)

几乎什么都没有...我试图检查那里网站上的文档.我认为我不需要任何身份验证.以前,我曾使用bs4的请求在页面上查找链接和信息,但从未尝试过输入.

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.

感谢您的帮助

推荐答案

关于下载"链接(简单)

只需请求网址

concerning the 'download' link (easy)

Just make a request to the url

当您单击<form>元素内的提交"按钮时,浏览器将从表单内所有<input>元素中收集所有数据,在这种情况下,它将收集文本输入的内容(将其放置在url中)的视频. 然后,浏览器使用表单"method"属性中指定的方法,向表单"action"属性中指定的URL发送请求.

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 .

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

您必须做什么

您必须像浏览器一样发出请求.您现在有了要转换的视频的URL 您必须使用表单中指定的方法(以本例中的'/'-我认为这是相对于页面当前URL的相对地址,但我不确定)来创建HTTP请求案例GET). 有关如何操作,请参见文档发送表单编码的数据

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})

额外

在浏览器的开发人员工具"中,您可以捕获单击转换视频"时浏览器发送的HTTP请求,这可能会有所帮助

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

这篇关于如何输入值并单击带有请求的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆