如何将表单提交到服务器(aspx)并从服务器(aspx)获取csv文件与python?

时间:2014-07-25 03:12:12

标签: python asp.net csv python-requests

我需要向服务器提交一个表单,并通过python从服务器获取csv文件。

服务器网站是(http:// 222.158.245.253/obweb/data/c1/c1_output6.aspx?LocationNo=0​​12),它发布了日本海上观测数据。

这是html源代码。                 

            <html xmlns="http://www.w3.org/1999/xhtml" >
            <head><title>
                大阪湾水質定点自動観測データ配信システム
            </title><link href="../../css/style.css" rel="stylesheet" type="text/css" />
                <script type="text/javascript" language="JavaScript" src="../../js/javascript.js" charset="Shift_JIS"></script>

              <script language="JavaScript" src="../../js/jkl-calendar.js" charset="Shift_JIS"></script>
              <script language="JavaScript">
                  var cal1 = new JKL.Calendar("calid1", "form1", "dummy");
                  var cal2 = new JKL.Calendar("calid2", "form1", "dummy");

                  function checkUpdate() {
                      return checkInputC1("6");
                  }
              </script>
            </head>
            <body>
                <form name="aspnetForm" method="post" action="c1_output6.aspx?LocationNo=012" id="aspnetForm">
            <div>
            <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjg3MjkzMjk3D2QWAmYPDxYOHghMT0NBVElPTgUDMDEyHglGSUxFX1BBVEhlHhJQQUdFX1NUQVJUSU5HX0RBVEUGY1KHxjd10YgeCUNPTkRJVElPTmQeC0xPR19NQU5BR0VSMvQCAAEAAAD/////AQAAAAAAAAAMAgAAAEBvc2tCYXlXZWIsIFZlcnNpb249MS4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1udWxsBQEAAAAbb3NrQmF5V2ViLkNvbW1vbi5Mb2dNYW5hZ2VyBQAAAAltX2xvZ1BhdGgJbV9sb2dGaWxlDW1fbWF4RmlsZVNpemUVbV9tYXhIaXN0b3J5RmlsZUNvdW50DG1fb3V0cHV0TW9kZQEBAAAECAgpb3NrQmF5V2ViLkNvbW1vbi5Mb2dNYW5hZ2VyK0xvZ091dHB1dE1vZGUCAAAAAgAAAAYDAAAAC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YQ/poqjlkJHjg7vpoqjpgJ8P5rWB5ZCR44O75rWB6YCfFQkCMTcBMQEyATMBNQE2ATcCMTYCMTUUKwMJZ2dnZ2dnZ2dnZGQCAw8QZBAVBQQyMDEwBDIwMTEEMjAxMgQyMDEzBDIwMTQVBQQyMDEwBDIwMTEEMjAxMgQyMDEzBDIwMTQUKwMFZ2dnZ2dkZAIJDxBkEBUFBDIwMTAEMjAxMQQyMDEyBDIwMTMEMjAxNBUFBDIwMTAEMjAxMQQyMDEyBDIwMTMEMjAxNBQrAwVnZ2dnZ2RkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBRhjdGwwMCRjcGhCb2R5JGJ0bkdldERhdGFZxKhgybCe8EJFmzXgcr7nV8hdETbkSUWgsjWYWJkHuA==" />
            </div>

            <div>

                <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAGxgIckqO32YNebWBRySsLFFBveULbFmThl5pJf/KPbRWtKSiJlLLGK+pS8Ap4vLbnfm0ibsOlAnQic7zwCoEreFp/HzVueR3sYZyY1Z+RWpwg1R+Wx8nSD80tiwfGNdYO1X+eWsrt0oVb3lRR/qn2Thbfy+KRXHTcRFxMfeaSyHT5fbMDouK5htbse0L/3sHyHvzyX53A8BjWuKIAOgPC9sRG65qQkIxN9d0Vkf77TVPwWd9CGfGCoL/haQUHSmUPJSDNkZuFchfFhTFm9myV/HjSR7l42Cp1HUqPiy97N9Cjkp6YOFsud5Ay0q9u+PyMgaKCDh5YLPVRbt/vOdb6TrLH46LLloLTAbA7+TjfiBaElZ5pmoQx/ZMbJclxEgDJFfhd/7nPqVjXsGEWj4EPnDB6lHUQPEBfo53llfG50tPheNGzrYR30iAjVO9r9TEruChpRSDsNavKoaAqEH8SdLJ4n3tVagxhDMrOv59ogdjMIyMKiTncgbFDiqShitLvf1/w9TJHCIoSkefIbhpMibDbhxxLqNFfSF4hszKM8YiwXsh+xPF5rb61vktn+Vw5oKmVh7A1vtt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X+Axf6LIU0byOI1kiIouXVHVgzNyiSJeFhKEv0xPMVinrhlamXjNOf1VmZTCLL8pSRh9eO79Fh3MFG3QnVF7co5YRizjuMu8X5d8jmqgwybDH30rFvRFK+8kxRCV4POEkh51WHszFi6yFR+TzYIk7cNyuf01k6N6liY6/XplVl4DNIjSA8KLbh5SFA9N/gfH8iaW0xWXEcYQ6Uwjtt0TMbW8h67XKlkeLk5u9C8oBKN3F6FgvWYPLQ0mXp7hXkKHtr22FE92GNOkgPVcxU+4HnfpHv+jeQbFp1rbHGtBkqI+A6oY/Fo3efxIoUkkXAusZqF5pVL1WfqB4UNhm4IoOindBovED5BBT5FnshkZimVIEbVOKyVYShB3DoYE36ccPt7RF4nNS4Rpf9iSV1J/Gca+X5aEEcWi6QulKImR071LnrU+CVvgrPTjcHDbwJvesqpMuzFLyjhV3X+roNOlyb8kaDE/oQfmu4XgfVepZjqlS301Cq94peNwbzutX1PYIlOuDFCHRbrU6TFU7cbi3o6kmDSSRNyeD9DHa0pTj1jLP/ffHjBSLHD8IIAp3e5nmVomYqy57yO/AH6semAqE6yUSYbjOauqPahlD8kLyZ3MDwTkcn2IIc+IxWeuT7QDzCXt/tsh9HsHc+bTWJKjrq4GCnS4wCe9WA1ijMikGk22Mq8sIMzZg==" />
            </div>
                <table id="output_main" width="100%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td valign="top">
                      <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                          <td valign="top">
                            <span id="ctl00_lblTitle" class="output_page_label_1">大阪港波浪観測塔 データダウンロード</span>
                            <hr color="#0066CC">
                          </td>
                        </tr>
                        <tr>
                          <td><span id="ctl00_lblMessage" class="output_page_message"> </span></td>
                        </tr>
                      </table>
                    </td>
                  </tr>

                <tr>
                  <td valign="top">
                    <table width="100%" border="0">
                      <tr>
                        <td valign="top">
                          <table border="0">
                            <tr>
                              <td colspan="3">
                                <span class="output_page_label_1">条件指定</span>
                              </td>
                            </tr>
                            <tr>
                              <td width="70" height="30"><span class="output_page_label_2">観測項目</span></td>
                              <td>
                                <select name="ctl00$cphBody$cboKansokuKomoku" id="ctl00_cphBody_cboKansokuKomoku" style="height:22px;width:120px;">
                <option value="17">全ての水質項目</option>
                <option value="1">  水温</option>
                <option value="2">  塩分</option>
                <option value="3">  光量子</option>
                <option value="5">  DO飽和度</option>
                <option value="6">  濁度</option>
                <option value="7">  クロロフィルa</option>
                <option value="16">風向・風速</option>
                <option value="15">流向・流速</option>

            </select>
                              </td>
                            </tr>
                            <tr>
                              <td width="90" height="30" valign="middle">
                                <div style="float: left;">
                                  <span class="output_page_label_2">観測日</span>
                                </div>
                                <div style="float: right;">
                                  <img src="../../img/cal.png" class="link" onClick="cal1.reset(); cal1.write('ctl00_cphBody_cboKansokuYearFrom', 'ctl00_cphBody_cboKansokuMonthFrom', 'ctl00_cphBody_cboKansokuDayFrom');"><br>
                                  <div id="calid1"></div>
                                </div>
                              </td>
                              <td width="320">
                                <table>
                                  <tr>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuYearFrom" id="ctl00_cphBody_cboKansokuYearFrom" style="height:22px;width:60px;">
                <option value="2010">2010</option>
                <option value="2011">2011</option>
                <option value="2012">2012</option>
                <option value="2013">2013</option>
                <option selected="selected" value="2014">2014</option>

            </select>
                                      <span class="output_page_label_2">年</span>&nbsp;&nbsp;
                                    </td>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuMonthFrom" id="ctl00_cphBody_cboKansokuMonthFrom" style="height:22px;width:40px;">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option selected="selected" value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>

            </select>
                                      <span class="output_page_label_2">月</span>&nbsp;&nbsp;
                                    </td>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuDayFrom" id="ctl00_cphBody_cboKansokuDayFrom" style="height:22px;width:40px;">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option selected="selected" value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>

            </select>
                                      <span class="output_page_label_2">日 から</span>
                                    </td>
                                  </tr>
                                </table>
                              </td>
                            </tr>
                            <tr>
                              <td width="90" height="30" align="right">
                                <img src="../../img/cal.png" class="link" onClick="cal2.reset(); cal2.write('ctl00_cphBody_cboKansokuYearTo', 'ctl00_cphBody_cboKansokuMonthTo', 'ctl00_cphBody_cboKansokuDayTo');"><br>
                                <div id="calid2"></div>
                              </td>
                              <td width="320">
                                <table>
                                  <tr>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuYearTo" id="ctl00_cphBody_cboKansokuYearTo" style="height:22px;width:60px;">
                <option value="2010">2010</option>
                <option value="2011">2011</option>
                <option value="2012">2012</option>
                <option value="2013">2013</option>
                <option selected="selected" value="2014">2014</option>

            </select>
                                      <span class="output_page_label_2">年</span>&nbsp;&nbsp;
                                    </td>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuMonthTo" id="ctl00_cphBody_cboKansokuMonthTo" style="height:22px;width:40px;">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option selected="selected" value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>

            </select>
                                      <span class="output_page_label_2">月</span>&nbsp;&nbsp;
                                    </td>
                                    <td>
                                      <select name="ctl00$cphBody$cboKansokuDayTo" id="ctl00_cphBody_cboKansokuDayTo" style="height:22px;width:40px;">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option selected="selected" value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>

            </select>
                                      <span class="output_page_label_2">日 まで</span>
                                    </td>
                                  </tr>
                                </table>
                              </td>
                        <td>
                                <input type="image" name="ctl00$cphBody$btnGetData" id="ctl00_cphBody_btnGetData" src="../../img/get_data.png" onclick="return checkUpdate();" style="height:40px;width:92px;border-width:0px;" />
                        </td>
                            </tr>
                            <tr>
                              <td height="5"></td>
                            </tr>
                            <tr>
                              <td colspan="2" height="50" align="center">
                              </td>
                            </tr>
                          </table>
                        </td>
                        <td height="400">
                          <iframe id="ctl00_cphBody_downloadFrame" style="width: 0px; height: 0px" name="downloadframe" frameborder="no"></iframe>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>

                  <input name="ctl00$txtPageID" type="hidden" id="ctl00_txtPageID" value="GxTYgjyWPW" />
                </table>
                </form>
            </body>
            </html>

到目前为止,我总是选择项目和日期,然后单击右下角的下载按钮。然后,当显示文件保存对话框时,我保留服务器中的csv文件。

我想用python自动化这些手工劳动。我研究过python和web scraping,并使用过python模块(比如BeautifulSoup)。但是,由于“aspx”,该网站很难进行网页抓取。

这是我制作的python源代码。

import requests

url = "http://222.158.245.253/obweb/data/c1/c1_output6.aspx?LocationNo=012"
payload = {'LocationNo':'012',
           'ctl00$cphBody$cboKansokuKomoku':'3',
           'ctl00$cphBody$cboKansokuYearFrom':'2013',
           'ctl00$cphBody$cboKansokuMonthFrom':'6',
           'ctl00$cphBody$cboKansokuDayFrom':'1',
           'ctl00$cphBody$cboKansokuYearTo':'2014',
           'ctl00$cphBody$cboKansokuMonthTo':'6',
           'ctl00$cphBody$cboKansokuDayTo':'30'
          }

r = requests.post(url, data=payload)
r.text
print r.content
with open("requests_results.csv", "w") as f:
    f.write(r.text)
print r.content

但是,我无法获取保存obaserbation数据但可以获取html文件的csv文件。

请尝试这些手工劳动并帮助我自动完成这些工作。

1 个答案:

答案 0 :(得分:0)

我已使用urllib2模块:

import urllib
import urllib2

url = 'http://localhost:58123/webform1.aspx';
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

f = open('result.html', 'w')
f.write(the_page)
f.close()

它有效。但是我刚刚返回了服务器中生成的纯文本。您可以让您的Web服务器发回一个csv文件。