提交表格,并在 python 中使用机械获取结果

Submitting a form, and getting the result using mechanise in python

我正在尝试使用机械来提交网络表单并为我打印出结果。输入坐标来自 CSV 文件。

def form_scraper(xcord,ycord):
    br = mechanize.Browser()
    br.set_handle_refresh(False)
    br.open('http://www.whoi.edu/marine/ndsf/cgi-bin/NDSFutility.cgi?form=0&from=XY&to=LatLon')

    for form in br.forms():
        print 'Form name:', form.name

    br.select_form('XY2LLForm')

    br.form['Xcord'] = xcord
    br.form['Ycord'] = ycord

    br.form['DecLat'] = '18.10219969497151'
    br.form['DecLon'] = '79.00285162135349'

    response = br.submit()
    form_result = response.read()
    print form_result

这给了我以下错误并且没有产生任何输出。

Traceback (most recent call last):
  File "converter.py", line 31, in <module>
    form_scraper('1', '2')
  File "converter.py", line 20, in form_scraper
    response = br.submit()
  File "/usr/local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 541, in submit
    return self.open(self.click(*args, **kwds))
  File "/usr/local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 255, in _mech_open
    raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 404: Not Found

我错过了什么?

您正在使用的表单的 action 属性指定了一个不存在的位置。该表单没有提交按钮。当您在浏览器中使用该表单时,该表单起作用的原因是单击 "Convert" 按钮会运行一个 JavaScript 函数,该函数将进行实际计算并将结果写入底部的 table页面的。

恐怕您不能使用 mechanize 检索结果,因为它不处理 JavaScript。