Python:如何使用Request

时间:2018-12-09 08:27:50

标签: python forms python-2.7 cgi

我刚刚开始学习Python,并想编写一个脚本来提交Form。

我发现Form使用CGI,这里是Form:

    <div class="box" id="url_upload">

                <div class="tabcontent">
                   <div class="progress_div"></div>
                    <div class="reurlupload">
                            <div class="progress_div"></div>
                            <form method="post" id="uploadurl" action="https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url">
                            <input type="hidden" name="sess_id" value="xv71zsrmtr38oh3z">
                            <input type="hidden" name="utype" value="reg">
                            <input type="hidden" name="file_public" value="1">

                            <div class="leftintab">
                            <p style="margin:0px;">

                                You can enter up to <b>20</b> URLs, one URL per row</br>
                                Max file size is <b>10240 Mb</b>
                            </p>
                                <textarea name="url_mass" style="width:100%; margin-top: 10px;" placeholder="e.g. http://example.com/xxxxxxxxxx.xyz"></textarea>
                            </div>

我使用请求制作python脚本,如下所示:

#I have session with my login & password as cookie
#Go to form page
login = s.get('https://ayefiles.com/?op=upload_form')
login_html = html.fromstring(login.content)
hidden_inputs = login_html.xpath('//input[@type="hidden"]')
# Input query data
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form ['sess_id']= 'xv71zsrmtr38oh3z'
form['utype']= 'reg'
form ['file_public']= '1'
form['url_mass'] = longurl
# POST
login = s.post('https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url', data=form)
print (login.url)

我对login.url ==> ayefiles.com/?op=upload_result&st=OK&fn=xxxxx的预期结果

但是我的结果失败了,==> ayefiles.com/?op=upload_result&st=Torrent%20engine%20is%20not%20running& fn = undef

如何解决我的问题?我的代码有什么问题? 请为我提供正确的代码。

1 个答案:

答案 0 :(得分:0)

我在零件多形式数据方面的错误。

正确的代码:

form ={'sess_id':(None,'xv71zsrmtr38oh3z'),'utype':(None,'reg'),'file_public':(None,'1'),'url_mass':(None,longurl)}
login = s.post('https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url', data=form)