使用Python从网站下载JavaScript文件

3

我正在尝试使用Python从以下网站下载结果:

http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY

在意识到机械化不支持下载文件的 JavaScript 后,我试图使用机械化。迄今为止,我的代码打开了网页,如下所示。我卡在如何访问网页上的下载链接上,以便将数据保存到我的计算机上。

import urllib2

def downloadFile():

    url = 'http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY'
    t = urllib2.urlopen(url)
    s = t.read()
    print s

打印出来的结果是:
<html>
<head></head>
<body>
  <form name="apiForm" method="POST">
    <input type="hidden" name="rowids">
    <input type="hidden" name="annot">

    <script type="text/javascript">
      document.apiForm.rowids.value="4791928,3403495,....";   //There are really about 500 values
      document.apiForm.annot.value="48";
      document.apiForm.action = "chartReport.jsp";
      document.apiForm.submit();
    </script>

  </form>
</body>
</html>

有人知道我如何选择并移动到下载文件页面,将该文件保存到我的电脑上吗?


我的解决方案对您有效吗? - Jordan
1个回答

2

在进一步研究了该链接后,我得出了这个结论。你绝对可以使用 mechanize 来完成它。

import mechanize

def getJSVariableValue(content, variable):
    value_start_index = content.find(variable)
    value_start_index = content.find('"', value_start_index) + 1

    value_end_index = content.find('"', value_start_index)

    value = content[value_start_index:value_end_index]
    return value

def getChartReport(url):
    br = mechanize.Browser()
    resp = br.open(url)
    content = resp.read()
    br.select_form(name = 'apiForm')
    br.form.set_all_readonly(False)
    br.form['rowids'] = getJSVariableValue(content, 'document.apiForm.rowids.value')
    br.form['annot'] = getJSVariableValue(content, 'document.apiForm.annot.value')
    br.form.action = 'http://david.abcc.ncifcrf.gov/' + getJSVariableValue(content, 'document.apiForm.action')

    print br.form['rowids']
    print br.form['annot']

    br.submit()

    resp = br.follow_link(text_regex=r'Download File')
    content = resp.read()
    f = open('output.txt', 'w')
    f.write(content)


url = 'http://david.abcc.ncifcrf.gov/api.jsp?type=GENBANK_ACCESSION&ids=CP000010,CP000125,CP000124,CP000124,CP000124,CP000124&tool=chartReport&annot=KEGG_PATHWAY'
chart_output = getChartReport(url)

我尝试了不同版本。我收到的错误输出是: - Marea
跟踪回溯(最近的调用在前): File "<pyshell#0>", line 1, in <module> downloadFile() File "C:\Python27\DAVIDLink.py", line 14, in downloadFile content = br.follow_link(text_regex=r"Download File").read() File "C:\Python27\lib\mechanize.py", line 569, in follow_link return self.open(self.click_link(link, **kwds)) File "C:\Python27\lib\mechanize.py", line 553, in click_link link = self.find_link(**kwds) File "C:\Python27\lib\mechanize.py", line 620, in find_link raise LinkNotFoundError() LinkNotFoundError - Marea
根据我的理解,这是因为 mechanize 无法理解或解释网站主要使用的 JavaScript。@Jordan - Marea
啊,你说得对。我在自己浏览时不小心漏掉了那一步。我会看看能否找到解决办法。 - Jordan
@Marea好的,我编辑了我的答案,并附上了一个实际可运行和经过测试的示例。它将内容输出到一个名为output.txt的文件中,该文件与您从脚本运行的同一文件夹中。我相信您可以根据自己的需求进行修改。 - Jordan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接